wood burning stoves 2.0
The moose likes Beginning Java and the fly likes SetPrecision? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "SetPrecision?" Watch "SetPrecision?" New topic


Alfred Harre
Ranch Hand

Joined: Apr 29, 2004
Posts: 73
How can I set precision (say 2 decimal places) of a float ?
Anurag Mishra
Ranch Hand

Joined: Jun 16, 2003
Posts: 121

I don't know in what contest you want this, In case
you are showing some text input field and you want user to enter only
float till two decimal point the you can achieve this using java script.

In the function attached you need to pass the value entered by user in this Java script function
function validDecimal(str)

If User enters more than 2 decimal points, put an alert message.


SCJP 1.2
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Older versions of the JDK (pre 1.1 if I remember right) had a setPrecision method on floating point numbers. The idea was you could set the prescision of the number before you used it in any calculations. This is long since gone in favour of the java.text.NumberFormat stuff, which is precision for display only i.e. you would perform any calculations on the float as is, then format it for display to two decimal places.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
I agree. Here's the link: http://aspose.com/file-tools
subject: SetPrecision?
jQuery in Action, 3rd edition