aspose file tools*
The moose likes Beginning Java and the fly likes How to restrict the decimal place of a floating point number? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to restrict the decimal place of a floating point number?" Watch "How to restrict the decimal place of a floating point number?" New topic
Author

How to restrict the decimal place of a floating point number?

Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Hi,
Here is my code:

I want to show a floating point number which has 2 digits and 2 decimal places, e.g. 45.82, 29.67. This number is input by user and passed as a parameter.
For those case like the above sample floating point numbers, it can display the proper value of 'c'. e.g. 45.67 will display 5.
However, when I passed 99999, it will show 7; 9999 will return 6, not 5.
So, if the user does not input the '.', does it append 2 implicit chars to it? i.e. 99999.0 and 9999.0. So, that's why it returned 7 and 6 for the length of the string respectively.
How can I fix it?
and
Does it has better algorithm?
Pls advise.
Andrew
Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
BTW, (1) I must use double data type; (2) I cannot round the floating point number (so I cannot use DecimalFormat to handle it).
How can I fix it?
I tried to use charAt to read each element of char of the string. If the char(2) does not contain '.' and char(5) != null, then it returns false. However, it did not compile for the method (char, null) in the if statement.
What should I do it now?
Thanks in advance.
Andrew

Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
Often, when you are trying to validate user input, it is easier to do the validation against the String rather than after the conversion. A regular expression would handle this nicely. Maybe something like this: \d{1,2}(\.\d{0,2})?
Otherwise, think about bounds of the number you are looking for and validate it numerically: It is less than 100. That's a start.
Validating the "two decimal places" is going to be difficlut, since the floating point representation of numbers in a computer is inexact. You might try to multiply your number by 100 and then see if it had any fractional part. This will work often enough, but might fail sometimes even if the user entered something valid - because the floating point approximation of the number might not be exactly what they entered (it should be really really close, however).
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
I am a bit confused. Why check the length of the string.
Why don't you just check for an input value less that 100? Anything over that would have 3 digits.
Then just multiply the number by 100 and chop off the trailing decimals (Math.floor(w) will do that). Then divide again by 100 to put the decimal back where it belongs.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Thanks for both of your advice.
Bounding the number to be less than 100 is not appropriate.
e.g. 45.67 - accept
45.678 - rejected
If the input from user has more than 2 decimal place but within 100, it will require the user to input the number again.
How should I deal with this case?
Andrew
:roll:
Aniruddha Mukhopadhyay
Ranch Hand

Joined: Nov 15, 2000
Posts: 59
You can use the class java.text.DecimalFormat - refer Java API for details. Using this you can set the number of decimal places in output, method name appropriately called "setMaximumFractionDigits(int whatEverNumberYouWant)"


Aniruddha
 
 
subject: How to restrict the decimal place of a floating point number?