| Author |
To know more about float data type
|
Bharath Sundar
Greenhorn
Joined: Feb 19, 2009
Posts: 13
|
|
Hello,
I have a variable of the data type float, into which assigning a value from the DB column.
If the value in DB is 19.00 is assigned to the float variable and if its printed in the JSP page, its printing only 19.0. But I need the value as 19.00.
This problem is not occuring when the value in DB is 19.01. Please provide me ideas so that after the decimal point if there are tow 0's i need to populate the same value as such from DB.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
Please read the important administrative private message I just sent you.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24061
|
|
Hi,
Welcome to JavaRanch!
This question gets asked in one form or another pretty much every day. The short answer: if you want all your numbers to appear with two decimal places when you print them, then you must specifically format them that way. For example
will print "0.00".
|
[Jess in Action][AskingGoodQuestions]
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
Beware of floats; they only have something like 7.23 significant figures' precision in decimal at the very best. See the discussion on this recent thread for an example of the problems which can arise, even with the more precise double type.
And thank you for correcting the name.
|
 |
Bharath Sundar
Greenhorn
Joined: Feb 19, 2009
Posts: 13
|
|
hi,
First of all thanks for the reply. As you told that sysout works fine.
But in my case i have a float variable, whose value is set with the help of the setter in the VO from the database. In the database the value is 19.000, when i display the same in the JSP page, its printing only 19.0.
Suggest me how can I make the float variable to display as 19.000 in JSP instead of 19.0.
Regards,
Bharath.S
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24061
|
|
Hi,
I think you're not understanding. A float has no concept whatsoever of "number of decimal places." If you have a float that represents 19, then it's 19; it's not 19.0, or 19.00000, or anything else. The zeros are part of the printing, not the number itself. The value in the database is not 19.000, unless the value in the database is a String rather than an actual float.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9955
|
|
just to elaborate (because I like to do that sort of thing)
There is a difference between the 'value' of a number and the 'display' of a number. if you're DB contains a number, say 19, it represents the exact value (or something pretty close). However, you could display it as:
19
19.0
00019.00000
nineteen
XIX
dix-neuf (my french stinks, so forgive me if that's wrong)
0x13
or a bunch of other ways. Ernest is telling you that what you store in the DB is the value - the concept of 'nineteen'. How you display/print/format/output it is a separate issue. Don't use System.out.println(), but use System.out.printf() instead. It lets you control the format of the output.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
 |
|
|
subject: To know more about float data type
|
|
|