1. Introducing Channels - a new way to chat with other Android users!
    Dismiss Notice

Doing math on Android


Last Updated:

  1. luitron

    luitron New Member This Topic's Starter

    Joined:
    Oct 2, 2010
    Messages:
    4
    Likes Received:
    0
    I'm trying to learn how to do math on an android environment. Basicly what I am trying to do is convert a set of numbers into Years, Months and days. This is what I have so far:

    Code (Text):
    1.  
    2. protected void convertNumbersToTotalTime() {
    3.         double val = Double.parseDouble(numbers.getText().toString());
    4.         double val2 = (val  / 365);
    5.         double val3 = (val2 * 365);
    6.         double val4 = (val - val3);
    7.         double val5 = (val4 / 30);
    8.         double val6 = (val5 * 30);
    9.         double val7 = (val4 - val6);
    10.         String val8 = val2 + " Years " + val5 + " Months " + val7 + " days";
    11.         outputnum.setText(val8);
    12.        
    13.     }
    14.  
    The only problem is that the output is the years with a long set of decimals. So if I enter 1000 in the in the field I get "2.739726027397260273972602739726" years. In order for me to get the months I need to limit the decimals to so that all I am left is "2". I been looking around but I can't seem get it to work. Any thoughts?

    Any advice or recommendations will greatly be appreciated.
     

    Advertisement
  2. luitron

    luitron New Member This Topic's Starter

    Joined:
    Oct 2, 2010
    Messages:
    4
    Likes Received:
    0
    I think i posted this on the wrong forum, i think it should had been under "Application Development Dev Lounge for the Coder Folks"
     

Share This Page

Loading...