Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether a float number contains decimals or not

How can I check whether a float number contains decimals like 2.10, 2.45, 12382.66 and not 2.00 , 12382.00. I want to know if the number is "round" or not. How can I do that programmatically?

like image 483
Sam Avatar asked Jan 12 '11 11:01

Sam


People also ask

How do you check whether the number is decimal or not?

function number_test(n) { var result = (n - Math. floor(n)) !== 0; if (result) return 'Number has a decimal place. '; else return 'It is a whole number.

Can float contain decimals?

If doing math with floats, you need to add a decimal point, otherwise it will be treated as an int. See the Floating point constants page for details. The float data type has only 6-7 decimal digits of precision. That means the total number of digits, not the number to the right of the decimal point.

How do you check if a number is float or not?

Check if the value has a type of number and is not an integer. Check if the value is not NaN . If a value is a number, is not NaN and is not an integer, then it's a float.

How do you check if a number has a decimal in Python?

Python String isdecimal() The isdecimal() method returns True if all characters in a string are decimal characters. If not, it returns False.


1 Answers

Using modulus will work:

if(num % 1 != 0) do something!
// eg. 23.5 % 1 = 0.5
like image 123
Knights Avatar answered Oct 06 '22 03:10

Knights