*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "SetPrecision?" Watch "SetPrecision?" New topic
Author

SetPrecision?

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
hi,

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.





thanks
Anurag


SCJP 1.2
Paul Sturrock
Bartender

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
 
Don't get me started about those stupid light bulbs.
 
subject: SetPrecision?