Jump to content

Floating point ...~

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guests
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 ...!!!!!

#2
lamm

lamm

    Newbie

  • Members
  • Pip
  • 3 posts
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.

#3
lamm

lamm

    Newbie

  • Members
  • Pip
  • 3 posts
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

#4
Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guests
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 ..~