Check If Number Is Decimal

Check if a number has a decimal place/is a whole number

Using modulus will work:

num % 1 != 0
// 23 % 1 = 0
// 23.5 % 1 = 0.5

Note that this is based on the numerical value of the number, regardless of format. It treats numerical strings containing whole numbers with a fixed decimal point the same as integers:

'10.0' % 1; // returns 0
10 % 1; // returns 0
'10.5' % 1; // returns 0.5
10.5 % 1; // returns 0.5

How to check if number is a decimal?

Use the modulus operator to check if there is a remainder.

if(a % b != 0) Log.v("result", "The result is a decimal");
else Log.v("result", "The result is an integer");

Check if number is decimal

You can get most of what you want from is_float, but if you really need to know whether it has a decimal in it, your function above isn't terribly far (albeit the wrong language):

function is_decimal( $val )
{
return is_numeric( $val ) && floor( $val ) != $val;
}

How to determine if a decimal/double is an integer?

For floating point numbers, n % 1 == 0 is typically the way to check if there is anything past the decimal point.

public static void Main (string[] args)
{
decimal d = 3.1M;
Console.WriteLine((d % 1) == 0);
d = 3.0M;
Console.WriteLine((d % 1) == 0);
}

Output:

False
True

Update: As @Adrian Lopez mentioned below, comparison with a small value epsilon will discard floating-point computation mis-calculations. Since the question is about double values, below will be a more floating-point calculation proof answer:

Math.Abs(d % 1) <= (Double.Epsilon * 100)

Check if number is decimal with swift

If you round the number down (which you can do by using the floor function), and then subtract it from the original number, you will get the difference between the two.

if (number - floor(number) > 0.000001) { // 0.000001 can be changed depending on the level of precision you need
// decimal
}

Edit --

My original answer recommended calculating the difference between the number and its floored equivalent to see if there were any units after the decimal points. However, as later described, there may be a rounding error which causes the representation of a value in memory to be slightly different than what it's actually meant to be.

For example, 3.0 could be represented as 3.00000000000001, and therefore the number - floor(number) > 0 would return true, even though it should've theoretically returned false as the offset would be 0.00000000000001.

Therefore please use @jessy's answer below.

How to check if a number has a decimal place/is a whole number?

You are interested in what is called the scale of your BigDecimal.

Call BigDecimal#scale.

BigDecimal x = new BigDecimal( "45.5343434" );
BigDecimal y = new BigDecimal( "45" );

x.scale(): 7

y.scale(): 0

You asked:

I would like to limit the decimal places by 4

Test for the scale being larger than four. If so, round.

    BigDecimal num = new BigDecimal(45);
if (num.scale() > 4) {
num = num.setScale(4, RoundingMode.HALF_UP);
}
System.out.println(num);

Output:

45

In case of more decimals:

    BigDecimal num = new BigDecimal("45.5343434");

45.5343

You can choose a different rounding mode to fit with your requirements.

Check if the string is number is decimal or integer without using contains comma or dot

In your case, I would use NumberStyles, using NumberStyles.Integer might let your input be sure as an only number.

decimal.TryParse(value,NumberStyles.Integer,CultureInfo.CreateSpecificCulture("sv-SE"), out var eval)

c# fiddle

How to check if a float value is a whole number

To check if a float value is a whole number, use the float.is_integer() method:

>>> (1.0).is_integer()
True
>>> (1.555).is_integer()
False

The method was added to the float type in Python 2.6.

Take into account that in Python 2, 1/3 is 0 (floor division for integer operands!), and that floating point arithmetic can be imprecise (a float is an approximation using binary fractions, not a precise real number). But adjusting your loop a little this gives:

>>> for n in range(12000, -1, -1):
... if (n ** (1.0/3)).is_integer():
... print n
...
27
8
1
0

which means that anything over 3 cubed, (including 10648) was missed out due to the aforementioned imprecision:

>>> (4**3) ** (1.0/3)
3.9999999999999996
>>> 10648 ** (1.0/3)
21.999999999999996

You'd have to check for numbers close to the whole number instead, or not use float() to find your number. Like rounding down the cube root of 12000:

>>> int(12000 ** (1.0/3))
22
>>> 22 ** 3
10648

If you are using Python 3.5 or newer, you can use the math.isclose() function to see if a floating point value is within a configurable margin:

>>> from math import isclose
>>> isclose((4**3) ** (1.0/3), 4)
True
>>> isclose(10648 ** (1.0/3), 22)
True

For older versions, the naive implementation of that function (skipping error checking and ignoring infinity and NaN) as mentioned in PEP485:

def isclose(a, b, rel_tol=1e-9, abs_tol=0.0):
return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)

How to check if a Number is decimal or not and apply here?

int sum = (double)(s/2);

This takes s and divides it (as an integer) by 2 (which truncates), then casts the definitely integer result to a double, then stores the result back into an int, which truncates again. Consider

double sum = ((double)s) / 2.0;

But you can avoid going through double at all by checking the modulo, which tells you the remainder under division.

if (s % 2 != 0) return false;


Related Topics



Leave a reply



Submit