Getting decimal part of a float value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ssmvijay
    New Member
    • Mar 2013
    • 9

    Getting decimal part of a float value

    I would like to get the fractional part i.e., digits after the decimal point in a float value without using any built in functions.

    if float x=20.158;
    i want to store 158 in other variable.
    NOTE: should not use built in functions in java.

    Thanks in Advance.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    What have you tried?

    Comment

    • ssmvijay
      New Member
      • Mar 2013
      • 9

      #3
      I would like to know the logic to separate the numbers next to '.' in a float value.

      for example :

      float a=20.154;
      float b=158.75885;
      float c,d;
      c=a%1;
      d=b%1;
      System.out.prin tln(c); // value in c will be 0.1539999999999 9991
      System.out.prin tln(d); // value in d will be 0.7588499999999 954

      but want the value of c as just 0.154 or 154 and value of d as 0.75885 or 75885

      Thanks in advance

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Fractional values cannot be represented accurately in a binary based computer. You have to start with value as a string to get the a string that represents the exact value.

        Comment

        Working...