• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Doing math on Android

luitron

Lurker
Oct 2, 2010
4
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:
protected void convertNumbersToTotalTime() {
		double val = Double.parseDouble(numbers.getText().toString());
		double val2 = (val  / 365);
		double val3 = (val2 * 365);
		double val4 = (val - val3);
		double val5 = (val4 / 30);
		double val6 = (val5 * 30);
		double val7 = (val4 - val6);
		String val8 = val2 + " Years " + val5 + " Months " + val7 + " days";
		outputnum.setText(val8);
		
	}

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.
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones