• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusion regarding Decimal Format  RSS feed

 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to round off an user input double value to two decimal places for which I am using the Decimal Format class. This works fine until the user inputs the value 0. Then, instead of giving an output such as 0.00 (2 decimal places), I get 0.0. Why does this happen? Is there an alternative method?


 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to javadoc for DecimalFormat
0 Number Yes Digit
# Number Yes Digit, zero shows as absent

So, I think you want "0.00"
Haven't tried it though.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I tried this. It still gives the same output.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Output for '0'


Edit: Once you do Double.valueOf() you've lost any formatting.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. This really helped.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do people still use DecimalFormat when you can write this?
System.out.printf("%.2d%n", myDouble);
Don't copy‑and‑paste that code.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently not comfortable with the syntax and functionality of printf. I need some practice to get used to it. But, why do I get a run time exception:
       java.util.IllegalFormatPrecisionException: 2
at java.util.Formatter$FormatSpecifier.checkInteger(Unknown Source)
at java.util.Formatter$FormatSpecifier.<init>(Unknown Source)
at java.util.Formatter.parse(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at formattedValue.main(formattedValue.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
>

while implementing this:



Isn't line 8 supposed to round off the value to two decimal places?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The precision option isn't available for decimal integral types (%d). Perhaps, you meant to use the floating point type (%f) ?

Henry
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Henry
%f does the job here but what is %.2d supposed to do? What kind of values can it handle? I am sorry but I don't quite understand how printf works. Can I get some links/resources to gain a thorough understanding?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khusbu Sinha wrote:
%f does the job here but what is %.2d supposed to do? What kind of values can it handle?


The precision field is not applicable with integral types. As for what is an integral type, they are (can be applied to) ... byte, Byte, short, Short, int and Integer, long, Long, and BigInteger (see JavaDoc link below).

Khusbu Sinha wrote:Can I get some links/resources to gain a thorough understanding?


The JavaDoc has a good chart / table that list everything...

https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

Henry
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khusbu Sinha wrote:I am currently not comfortable with the syntax and functionality of printf. . . .
It would have been a lot easier for you if I had got that format string right
Try the version with my error corrected:- And I am very sorry for my mistake. Not d but f.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Henry
thanks for the link.
@Campbell

This code will work only when I am to print the formatted value. I guess it won't work if I want it as a return value from a method. I still have to use Decimal Format in that case.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to return a value with a set number of decimal places use BigDecimal. You'll still need to format it when printing. If you want to pass around the formatted version you'd have to do that as a String. The String class has a format() method that behaves the same as printf() but generates a formatted String instead of printing it.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no such thing as a double with a specific number of decimal places. You would have to convert the double to a String and return a String with a decimal point two places from its right end. You cannot return a “formatted” double.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Carey
this was pretty helpful. Thanks.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell
yes, I got it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!