Forums Register Login

SetPrecision?

+Pie Number of slices to send: Send
How can I set precision (say 2 decimal places) of a float ?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
Sunglasses. AKA Coolness prosthetic. This tiny ad doesn't need shades:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 5894 times.
Similar Threads
Float
Mistake in Calculation of Percentage
Single precision /double precision?
Working with floating point numbers in for loops and arrays
float/double decimal representation ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:16:22.