Determine million, billion, trillion, Quadrillion in JAVA -



Determine million, billion, trillion, Quadrillion in JAVA -

from standard dictionary numbers, need fastest way convert number bellow :

1000000 = 1 1000000 1435234 = 1.43 1000000 350000000 = 350 1000000 1000000000 = 1 billion 1765000000 = 1.76 billion 1000000000000 = 1 trillion 1345342345000 = 1.34 trillion 1000000000000000 = 1 quadrillion 100000000000000000 = 100 quadrillion

and further.

i have tried bellow :

public string truncatenumber(float floatnumber) { long 1000000 = 1000000l; long billion = 1000000000l; long trillion = 1000000000000l; long number = math.round(floatnumber); if ((number >= million) && (number < billion)) { float fraction = calculatefraction(number, million); homecoming float.tostring(fraction) + "m"; } else if ((number >= billion) && (number < trillion)) { float fraction = calculatefraction(number, billion); homecoming float.tostring(fraction) + "b"; } homecoming long.tostring(number); } public float calculatefraction(long number, long divisor) { long truncate = (number * 10l + (divisor / 2l)) / divisor; float fraction = (float) truncate * 0.10f; homecoming fraction; }

but think solution not quite true. so, fastest way in java? many thanks.

the first problem float not have plenty precision represent these numbers. in fact, double not have plenty precision values in nonillion range - although may not of import here, because want drop of info of number anyhow.

nevertheless, have implemented here using biginteger. converting utilize double should straightforward, if don't care precision issues.

the basic thought here create navigablemap powers of 1000 respective number name. map can looked using floorentry find best matching powerfulness (and thus, number name).

import java.math.biginteger; import java.util.map.entry; import java.util.navigablemap; import java.util.treemap; public class numbernames { public static void main(string[] args) { test("100", "nearly nothing"); test("1000", "1 thousand"); test("1230", "1.23 thousand"); test("1000000", "1 million"); test("1435234", "1.43 million"); test("350000000", "350 million"); test("1000000000", "1 billion"); test("1765000000", "1.76 billion"); test("1000000000000", "1 trillion"); test("1345342345000", "1.34 trillion"); test("1000000000000000", "1 quadrillion"); test("100000000000000000", "100 quadrillion"); test("1230000000000000000000000000000000000000000000000000000000000000", "1.23 vigintillion"); } private static void test(string numberstring, string string) { biginteger number = new biginteger(numberstring); system.out.println(number+" "+createstring(number)+" should "+string); } private static final string names[] = new string[]{ "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion", "quattuordecillion", "quindecillion", "sexdecillion", "septendecillion", "octodecillion", "novemdecillion", "vigintillion", }; private static final biginteger one thousand = biginteger.valueof(1000); private static final navigablemap<biginteger, string> map; static { map = new treemap<biginteger, string>(); (int i=0; i<names.length; i++) { map.put(thousand.pow(i+1), names[i]); } } public static string createstring(biginteger number) { entry<biginteger, string> entry = map.floorentry(number); if (entry == null) { homecoming "nearly nothing"; } biginteger key = entry.getkey(); biginteger d = key.divide(thousand); biginteger m = number.divide(d); float f = m.floatvalue() / 1000.0f; float rounded = ((int)(f * 100.0))/100.0f; if (rounded % 1 == 0) { homecoming ((int)rounded) + " "+entry.getvalue(); } homecoming rounded+" "+entry.getvalue(); } }

java

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -