Results 1 to 5 of 5
  1. #1
    fataguila is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default Store the decimal number into an variable

    How can I store the decimal point of a number into a variable. Let's say I have 32.6 is there anyway to store the .6 into a variable or an array. Thanks any help would be greatly appreciated.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    I would try to use the Math.truncate facility, to obtain the whole number part of the original number, and then subtract that from it, the remaining part will be the decimal place

  3. #3
    fataguila is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    Thanks it worked out although I think i'm just gonna make the double variable into an integer and it gets truncated:

  4. #4
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    Thanks it worked out although I think i'm just gonna make the double variable into an integer and it gets truncated:
    This is not a very good idea. If you say:
    Java Code:
    double a = 32.6;
    int b = (int)a;
    double c = a-b;
    c = 0.6000000000000014

    As you can see this would most likely mess up some stuff in your code.

  5. #5
    fataguila is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    Well not really just did it
    Java Code:
            
            int Num1;
            String lengthString = Integer.valueOf(Num1).toString();
            int Vec[]=new int[lengthString.length()];
            double x1=Num1;
            double x2;
            int c=lengthString.length();
            for (int a=0; a<lengthString.length(); a++)
            {
                x2=x1/10;
                x2=(int)x2*10;
                c--;
                Vec[c]=(int)x1-(int)x2;
                x1=x2/10;
            }
    
    
    
    
    
    
    Vec[]=Num1
    Last edited by Fubarable; 02-01-2010 at 08:31 PM. Reason: code tags added

Similar Threads

  1. Replies: 7
    Last Post: 05-06-2009, 05:32 PM
  2. Replies: 1
    Last Post: 11-03-2008, 03:38 PM
  3. how to store the previous of a variable
    By Randa in forum New To Java
    Replies: 7
    Last Post: 09-22-2008, 09:16 PM
  4. Replies: 2
    Last Post: 12-05-2007, 02:19 PM
  5. Converts a binary number to a decimal
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 10:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •