2

I am looking for a NumberFormat configuration that format with signifant digits only applied to the fraction parts (and not on both integer and fraction parts).

Here are the expected results for the following examples (2 significants applied on the fraction part) :

0.00193 ---> 0.0019
0.46000 ---> 0.46
0.46365 ---> 0.46
0.46987 ---> 0.47
0.40000 ---> 0.40
0.40003 ---> 0.40
1.02000 ---> 1.02
1.02456 ---> 1.02
1.00000 ---> 1.00
1.00003 ---> 1.00
1.10000 ---> 1.10
125.1567 ---> 125.16
125.0005 ---> 125.00
142.1 ---> 142.10

Lot of solutions exist to format with significants digits, but offen applied to the integer part too. I think the solution would be a mix between the use of DecimalFormat and a BigDecimal.

Thanks for your answers ;)

Rizen
  • 121
  • 4
  • DecimalFormat df = new DecimalFormat("#.00"); is this not helping ? – shikjohari Jan 21 '15 at 10:00
  • 1
    AFAIK, a zero "in between" is just as significant as any other digit, so `0.40003` should be rounded to `0.40` IMHO, and similar for other numbers. – tobias_k Jan 21 '15 at 10:05
  • @shikjohari : No, it's not about a minimum fraction digit but significants digits.Particular examples i gave 0.40003 that must become 0.40003 or 1.02456 to 1.025. – Rizen Jan 21 '15 at 10:07
  • 1
    Why does `0.46365` result in `0.46` but `0.40003` results in `0.40003`? Based on what condition do you want 2 decimals and when do you want 4? –  Jan 21 '15 at 10:24
  • @a_horse_with_no_name : you're completely right. I made a big misunderstanding by applying [the significant rules](http://www.usca.edu/chemistry/genchem/sigfig.htm). I edit the example with the needed result. – Rizen Jan 21 '15 at 11:13
  • I think it's worse now: `0.00193 ---> 0.0019` but `1.00003 ---> 1.00` – cy3er Jan 21 '15 at 11:36

0 Answers0