1

To remove useless zeros after the point, I use the following method

if (("" + unitValue).endsWith(".0")) {
      return String.format("%.0f %s", unitValue, unitFaName);
    } else if (unitValue < 0.001) {
      return String.format("%f %s", unitValue, unitFaName);
    } else {
      return String.format("%s %s", String.valueOf(unitValue), unitFaName);
    }

This method has edited the following values :

1.000000 => 1

2 zero after the point and before one
1.001000 => 1.001

But can not modify the following values:

But more than two zero after the point and before one can not be modified

1.0001000
1.0000100
... 

That's why I used the following condition to avoid scientific notation top Numbers (1.0001000 , 1.0000100)

if(unitValue<0.001){
    return String.format("%f %s", unitValue, unitFaName);
}

In what way do I modify the following numbers?

more than two zero after the point and before one

1.0001000
1.0000100
... 

I can not use Decimalformat or String,Format because Digits after the point is not fixed numbers

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Delay
  • 93
  • 3
  • 9

1 Answers1

0

What you are looking for is answered here.

public static String fmt(double d)
{
    if(d == (long) d)
        return String.format("%d",(long)d);
    else
        return String.format("%s",d);
}
Community
  • 1
  • 1
Ivan
  • 3,781
  • 16
  • 20
  • The second answer in that link is better. Others apparently agree, since it has more votes. --- When the question has already been answered elsewhere, don't copy the answer. Close the question as a duplicate. – Andreas Oct 21 '16 at 16:13
  • @Andreas that not something I can do now – Ivan Oct 21 '16 at 16:26