kwan_ah
kwan_ah

Reputation: 1091

Why java converts double to exponential form

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.

Upvotes: 6

Views: 9510

Answers (3)

Stanley
Stanley

Reputation: 5127

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.

Upvotes: 6

Mohammod Hossain
Mohammod Hossain

Reputation: 4114

follow BigDecimal

BigDecimal bd = new BigDecimal(inputVal);
System.out.println(bd);

Upvotes: 7

Aviram Segal
Aviram Segal

Reputation: 11120

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.

Upvotes: 9

Related Questions