0

I am getting a value from server that is not containing any Floating point let say its 1234 and have to cvonvert it in Floating value with 2 decimal point like 12.34.

Right now what i am doing is getting value storing it in float that convert the current value 1234 to 1234.0 after that doing this

tempB=Math.floor(tempB)/100.0;
DecimalFormat df = new DecimalFormat("###.##");
RewardsBalance=df.format(tempB);

But with this i m having an issue that when i have value such that 1230 it results in 12.3 not that 12.30 but when i have value 1234 it gives the desired result that is 12.34 so what step i m missing any clue

Usman Kurd
  • 7,212
  • 7
  • 57
  • 86

4 Answers4

1

12.3 and 12.30 are the same value. The problem is not the value but the code that incorrectly converts the right value to the wrong representation. You probably want "###.00". With "#", zero shows as absent.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • so you mean i should use format like "###.00" despite using ###.## ? – Usman Kurd Aug 12 '13 at 05:56
  • You should use '#' if you want a digit to appear unless it's a meaningless zero. You should use '0' if you want a digit to appear even if it's a meaningless zero. That's what the [docs](http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html) say (see the section labelled "Special Pattern Characters"). – David Schwartz Aug 12 '13 at 05:59
1

Use this it will work

Two digits after point

Community
  • 1
  • 1
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
0

Try this :

String.format("%.2f", your_value);

It will do just like you want

Harish Godara
  • 2,388
  • 1
  • 14
  • 28
0

try this

tempB=Math.floor(tempB)/100.0;
DecimalFormat df = new DecimalFormat("0.00");
df.format(tempB);

it will work fine.

balaji koduri
  • 1,321
  • 9
  • 25