Kotlin: How do i check if a number has decimal points (modulus solution doesn't work)

how to get value before decimal point in java
kotlin bigdecimal setscale
how to check decimal value in java
kotlin decimal type
get decimal part of a number
round to 4 decimal places kotlin
how to separate decimal value in java
how to round a number to n decimal places in java

I'm new to Kotlin and came from JS. I am currently making a calculator app and am working on the '%' operator. I need to find out if the output of the 'current input * 0.01' is a whole number or with decimal points. Usually, I would use

num % 1 !== 0

but it does not work in Kotlin and gives me the error "!= operator cannot be applied to Double or Int". It is the same for Strings or Characters. My Kotlin code is below, hope someone can help! Thanks!

val percentResult: Double() = result.toDouble() * 0.01
  if(percentResult % 1 != 0) {
   result = (NumberFormat.getInstance().format(percentResult)).toString()
  } else {
   result = percentResult.toInt().toString()
  }

Equivalent code

The 0 is an int, so you need to explicitly say you want a double like this:

fun factor100(n: Number) = n.toDouble() % 100.0 == 0.0
Why this probably is not what you want

For double values this may not work correctly, due to floating point errors, so you will want to check if the difference is smaller than some small amount.

An example of how this is broken is this:

fun main(args: Array<String>) {
    var x = 0.3 - 0.2         // 0.1 (ish)
    x *= 1000                 // 100 (ish)
    println(factor100(x))     // False
}

fun factor100(n: Number) = n.toDouble() % 100.0 == 0.0

Determine if a string is numeric, Kotlin: How do i check if a number has decimal points (modulus solution doesn't work). kotlin format decimal places kotlin check if number is whole kotlin check if � The decimal module provides support for fast correctly-rounded decimal floating point arithmetic. It offers several advantages over the float datatype: Decimal “is based on a floating-point model which was designed with people in mind, and necessarily has a paramount guiding principle – computers must provide an arithmetic that works in the same way as the arithmetic that people learn at


See https://stackoverflow.com/a/45422616/2914140.

num % 1.0 != 0.0 // But better compare with a small constant like 1e-8.

For currency:

num.absoluteValue % 1.0 >= 0.005 (or other small constant).

Number of digits before the decimal point in the division of two , So, 1.34567 rounded to 3 decimal places prints 1.346, 6 is the next number Kotlin does not offers any standard function to check if the given string is I am using Kotlin but the Java solution is also helpful. val number:Double = 0.0449999 . How to check (determine) whether a number is integer or decimal in Python is explained with sample code in the following cases. Check if object is int or float: isinstance() Check if float is integer: is_integer() Check if numeric string is integer; If you want to get velues of the fractional and integer parts, see the following post.


In Kotlin you can use the "rem" function:

if (number.rem(1).equals(0.0))

Check whether given floating point number is even or odd , 29.1 Alternative solution 61 jq; 62 Julia; 63 Kotlin; 64 LabVIEW; 65 Lasso; 66 Liberty BASIC number. Returns true if it does, false otherwise % logical procedure A variable containing only digits (with an optional decimal point) is Or you could check out the String::Scanf module on the CPAN instead. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.


I don't know if im just lazy searching how to do this with core functions or there are no solutions about it. So i solved it doing this:

fun Double?.checkDecimals(decimals: Int): Boolean {
    if (this == null) return false
    val comparator = 10.0.pow(decimals)
    val doubleTimesComparator = (this * comparator)
    val integerUnit = doubleTimesComparator.toInt()
    return doubleTimesComparator == integerUnit.toDouble()
}

*android

Kotlin Program to Check Whether a Number is Even or Odd, The task is to find the number of digits before the decimal point in a / b. Examples: Please try your approach on {IDE} first, before moving on to the solution. i have the text box, that accepts only numbers with decimal place. But i have to find if the user has entered the more than 2 digits after decimal point, how find the length after decimal place. Posted 12-Dec-13 20:47pm


Python round() function with EXAMPLES, We can check whether a integer is even or odd by dividing its last digit by 2. But in case of floating point number we can't check a given number is even or Recommended: Please try your approach on {IDE} first, before moving on to the solution. than it means decimal part of number is traversed and now we can check� Kotlin Round Double To 2 Decimal About the Author. round function in Java to round to two decimal places. 15, cause we round half up. Hi, For a report i am trying to sum double values using a variable the formula for the same is as follows The formula works fine for summing the BrigadeSubTotalAEC.


JavaScript Math: Check if a number is a whole number or has a , In this program, you'll learn to check if a number entered by an user is even or odd. This will be done using two variants of ifelse in Kotlin. See the SQLite Primer for a quick overview or refresher. What you'll learn. How to create and interact with a Room database to persist data. How to create a data class that defines a table in the database. How to use a data access object (DAO) to map Kotlin functions to SQL queries. How to test whether your database is working. What you'll do


How to use modulo (%) to see if number is divisible by 2 , It will return you a float number that will be rounded to the decimal Back; PI/PO � PP � SD � SAPUI5 � Security � Solution Manager � Successfactors � SAP Tutorials If the decimal places to be rounded are not specified, it is considered as In addition to the round() function, python has a decimal module that� Select to_char(1234.40, ‘9999999.99’) from dual TO_CHAR(1232.70,99999999.99) ----- 1232.70 As you can see the to_char only works partially. It keeps the two decimal places and adds a zero but then it also hads white space to the front of the number.