I have a problem. In my jsp page I have a textfield which accepts monetary value. When I enter a value such as 66777764
it becomes 6.6777764E7
in my textfield whenever there is a validation error. I know this won't affect the value saved in the database but I think its misleading/confusing to the user. Please help. Thanks in advance.
Asked
Active
Viewed 9,052 times
6

kwan_ah
- 1,061
- 1
- 12
- 18
-
7It's all in how you're formatting the `double`. Search for "[java format double](https://www.google.com/search?q=java+format+double)" and you'll get plenty of information. Also, **never** use `double` (or other floating point for monetary values. – Jonathon Reinhart Dec 19 '12 at 07:47
3 Answers
9
It seems you keep your number as double
so 66777764
is actually 66.777764.0
and it displays what you got.
You can use DecimalFormat to format the display of the number as you wish, for example:
double d = 1.234567;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d)); // will print 1.23
This will display the number with 2 digits after the point (66777764.00
), there are many options for the format, check the documentation for info.

Aviram Segal
- 10,962
- 3
- 39
- 52
-
The BigDecimal answers are still good recommendations about monetary. – Aviram Segal Dec 19 '12 at 08:41
7
follow BigDecimal
BigDecimal bd = new BigDecimal(inputVal);
System.out.println(bd);

Mohammod Hossain
- 4,134
- 2
- 26
- 37
6
Use BigDecimal to keep the monetary value.
double d = 66777764d;
BigDecimal bd = new BigDecimal(d);
System.out.println(bd);
Read the answer of this question to find out why you should use BigDecimal.
-
4And I'd like to add: Never ever use floating point number, i.e. double or float in Java, to hold monetary values. You'll get in trouble. – jmg Dec 19 '12 at 07:54