Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the decimal part of a float?

I need to extract the decimal part of a float number, but I get weird results:

float n = 22.65f; // I want x = 0.65f, but...  x = n % 1; // x = 0.6499996  x = n - Math.floor(n); // x = 0.6499996185302734  x = n - (int)n; // x = 0.6499996 

Why does this happen? Why do I get those values instead of 0.65?

like image 987
Cristian Avatar asked Feb 16 '11 13:02

Cristian


People also ask

How do you get the decimal part of a float in Python?

Using the modulo ( % ) operator The % operator is an arithmetic operator that calculates and returns the remainder after the division of two numbers. If a number is divided by 1, the remainder will be the fractional part. So, using the modulo operator will give the fractional part of a float.

How do you extract the decimal part of a number?

The TRUNC function simply truncates (i.e. removes) decimal values if they exist – it doesn't do any rounding. The TRUNC function returns the integer portion of the number which is then subtracted from the original value. The result is the decimal portion of the number.

How do you get just the decimal part of a number in Excel?

Select a cell and type this formula =A1-TRUNC(A1) (A1 is the cell you want to extract decimal value from) into the Formula Bar, and then press Enter key. Keep selecting the first result cell, and drag fill handle down to get all results. You can see the decimal values are extracted with sign as below screenshot shown.

How do you extract the decimal part of a number in Java?

To get the decimal part you could do this: double original = 1.432d; double decimal = original % 1d; Note that due to the way that decimals are actually thought of, this might kill any precision you were after.


1 Answers

float only has a few digit of precision so you should expect to see a round error fairly easily. try double this has more accuracy but still has rounding errors. You have to round any answer you get to have a sane output.

If this is not desireable you can use BigDecimal which does not have rounding errors, but has its own headaches IMHO.

EDIT: You may find this interesting. The default Float.toString() uses minimal rounding, but often its not enough.

System.out.println("With no rounding"); float n = 22.65f; System.out.println("n= "+new BigDecimal(n)); float expected = 0.65f; System.out.println("expected= "+new BigDecimal(expected));  System.out.println("n % 1= "+new BigDecimal(n % 1)); System.out.println("n - Math.floor(n) = "+new BigDecimal(n - Math.floor(n))); System.out.println("n - (int)n= "+new BigDecimal(n - (int)n));  System.out.println("With rounding"); System.out.printf("n %% 1= %.2f%n", n % 1); System.out.printf("n - Math.floor(n) = %.2f%n", n - Math.floor(n)); System.out.printf("n - (int)n= %.2f%n", n - (int)n); 

Prints

With no rounding n= 22.6499996185302734375 expected= 0.64999997615814208984375 n % 1= 0.6499996185302734375 n - Math.floor(n) = 0.6499996185302734375 n - (int)n= 0.6499996185302734375 With rounding n % 1= 0.65 n - Math.floor(n) = 0.65 n - (int)n= 0.65 
like image 172
Peter Lawrey Avatar answered Sep 25 '22 02:09

Peter Lawrey