i would like to ask how am i going to track the significant figure ..?
for instance the user enters a floating point number and my program input just need a 1 decimal significant . if the user enter 19.89 so my program have to prompt error message to the use unless the user enters 19.8 , my program will continue without any error message..~
is there any class that can i use to handle tis?
urgent ...!!!!!
Floating point ...~
Started by
Guest_R3.RyozKidz_*
, Jan 28 2010 09:36 PM
3 replies to this topic
#1
Guest_R3.RyozKidz_*
Posted 28 January 2010 - 09:36 PM
Guest_R3.RyozKidz_*
|
|
|
#2
Posted 29 January 2010 - 02:39 AM
I would solve this problem in either of the following two ways:
1.Check the string that the user enters and test if there is only one digit following the decimal period.
or, alternatively,
2.Multiply the given floating number, say x, by 10, giving y, round this to the nearest integer, say k, and check if Math.abs(y - k) is less than a very small positive number, say 0.000001.
1.Check the string that the user enters and test if there is only one digit following the decimal period.
or, alternatively,
2.Multiply the given floating number, say x, by 10, giving y, round this to the nearest integer, say k, and check if Math.abs(y - k) is less than a very small positive number, say 0.000001.
#3
Posted 29 January 2010 - 04:52 AM
Using my second way of solving your problem, you may use this static method:
static boolean hasOneDecimal(double x)
{ double y = 10.0 * x;
int k = (int)Math.round(y);
return Math.abs(y - k) < 0.00000001;
}
I tested it with some arguments and obtained this output:
hasOneDecimal(19.81)=false
hasOneDecimal(19.79)=false
hasOneDecimal(19.8)=true
hasOneDecimal(19)=true
hasOneDecimal(-19.81)=false
hasOneDecimal(-19.79)=false
hasOneDecimal(-19.8)=true
hasOneDecimal(-19)=true
static boolean hasOneDecimal(double x)
{ double y = 10.0 * x;
int k = (int)Math.round(y);
return Math.abs(y - k) < 0.00000001;
}
I tested it with some arguments and obtained this output:
hasOneDecimal(19.81)=false
hasOneDecimal(19.79)=false
hasOneDecimal(19.8)=true
hasOneDecimal(19)=true
hasOneDecimal(-19.81)=false
hasOneDecimal(-19.79)=false
hasOneDecimal(-19.8)=true
hasOneDecimal(-19)=true
#4
Guest_R3.RyozKidz_*
Posted 29 January 2010 - 06:02 AM
Guest_R3.RyozKidz_*
there is a solution my lab tutor gave me ..~
1.user enter the number , scan it as double type
2.multiply the number to 10
3.and trying to use modulus 1 on the floating point
4.and the remainder is in between 0 <= x and x < 1
is there a prospect to use modulus on the floating point..?
as my lab tutor succeeded on using the modulus ..~
i heard that there also a class that also provide these kind of function but i forget what the class ..? as i remember , we need to pass a constructor like this to initialize the instance variable .. ("#0.00")
somethings like this ..~
1.user enter the number , scan it as double type
2.multiply the number to 10
3.and trying to use modulus 1 on the floating point
4.and the remainder is in between 0 <= x and x < 1
is there a prospect to use modulus on the floating point..?
as my lab tutor succeeded on using the modulus ..~
i heard that there also a class that also provide these kind of function but i forget what the class ..? as i remember , we need to pass a constructor like this to initialize the instance variable .. ("#0.00")
somethings like this ..~


Sign In
Create Account

Back to top










