1

Edited my question:

what is best way to extract number that is coming after the decimal places. I tried few solutions, but it not going to help me

float n = 67.7345f;
System.out.println(n % 1);
System.out.println(n - Math.floor(n));
System.out.println(n - (int)n);

here is the output:

0.7344971
0.7344970703125
0.7344971

But I want the exact 7345

Any best way to get the exact number after decimal point?

  • `\d+\.(\d+)` might help you (after converting the `float` to `String`). – Maroun Apr 13 '15 at 11:55
  • 2
    `%.2f`will only print 2 decimals.... `%.4f` would work better in your case! – assylias Apr 13 '15 at 11:55
  • 2
    You are currently limiting the output to 2 digits after the dot (with `%.2f%`). Simply remove the `.2` and you'll get all digits after the dot (or use a wider limit, such as `.6` or something like that) – Ori Lentz Apr 13 '15 at 11:56
  • @MarounMaroun very tricky. Output of floats can differ dependently on locale. – Alex Salauyou Apr 13 '15 at 11:56
  • It sounds like you just do not know about the %.2f or %.4f formats. If what you actually want is a regex, then please say so more explicitly and I will be glad to reopen your question for you. Be warned though that using a regex is probably the nastiest way to go about this. – tchrist Apr 13 '15 at 13:37

4 Answers4

3

Here is the code that will print all the digits you mention:

 float n = 67.7345f;
 System.out.printf("n %% 1= %.4f%n", n % 1);
 System.out.printf("n - Math.floor(n) = %.4f%n", n - Math.floor(n));
 System.out.printf("n - (int)n= %.4f%n", n - (int)n);

The main point is using %.4f.

Have a look at the sample program output.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
2

But I want the exact 7345

So you can simply use the %.4f.

%.2f will limit to 2 places of decimal.

Any best way to get the exact number after decimal point?

When you are dealing with floating point numbers then there is certainly few issues with the floating point numbers. floatis a 32-bit precision IEEE 754 floating point. A good read What Every Computer Scientist Should Know About Floating-Point Arithmetic

I dont know if thats the best way or not but here is one approach for dealing with rounding of floating point numbers:

public static BigDecimal roundFloat(float x, int roundTodecimalPlace) 
{
    BigDecimal b = new BigDecimal(Float.toString(x));
    b = b.setScale(roundTodecimalPlace, BigDecimal.ROUND_HALF_UP);       
    return b;
}

Then call it like

  float n = 67.7345f;
  BigDecimal i;
  i=roundFloat(x,4);
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

You can use java.math.BigDecimal to store the data exactly. Then it's possible to print the exact digits after the decimal point.

With float it is not possible to print the exact number, because you do not know how many digits the initial number had.

So be sure, test an answer with:

float n = 67.7f;
float n = 67.73f;
float n = 67.734f;
float n = 67.7345f;

String floatString = Float.toString(n) // could work, if no calculation was made with the float variable.

BigDecimal number = new BigDecimal("67.734");
BigDecimal result = number.subtract(new BigDecimal(number.longValue()));
System.out.println(result); // 0.734
test
  • 86
  • 4
  • What are you going to all that work when `printf` is the obviously correct answer here? – tchrist Apr 13 '15 at 12:51
  • If the developer has knowledge about the length of the float then printf works well. It was my mistake to think printf would not round correctly. I do not see the requirement to fill digital places with zeros. – test Apr 13 '15 at 13:13
0

If you use numWithDecimals%100 it will remove the first 2 values and you will just have the decimals :)

10 Replies
  • 426
  • 9
  • 25