开发者

Show Normal Number Instead of Exponential Form

I want to show number in a Normal form instead of Exponential开发者_如何学Go Form

For Example My Number is stored in a double variable with value 1234567890123

I want to get the exact representation. but when I pass it to some TextView for display, it becomes 1.234E12


Try Out with the Big decimal class in java..

Big decimal class has the advantage of some inbuilt Rounding function which you can use for example:

Double a = 7.77d * 100000000; //Multiply Operation

System.out.println("Large multiply " + a.doubleValue());

System.out.println("Magic of big decimal " + BigDecimal.valueOf(a).setScale(0,RoundingMode.HALF_EVEN).toPlainString());

a = 7.77d / 100000; //Devide Operation

System.out.println("Devide operation " + a.doubleValue());

System.out.println("Magic of big decimal " + BigDecimal.valueOf(a).toPlainString());

DecimalFormat formatter = new DecimalFormat("0.000000");

System.out.println("Trimming the big string : "+formatter .format(a)); 

Output :

Large multiply 7.77E8

Magic of big decimal 777000000

Devide operation 7.769999999999999E-5

Magic of big decimal 0.00007769999999999999

Trimming the big string : 0.000078


You can try this:

double v = 1234567890123d;
Double d = new Double( v );

Now when you pass it to TextView, you can pass it as follows (assuming that you are interested only in the integer part of a double):

d.longValue();

Now, why is it giving you 1.234E12: The rules of printing doubles can be found here (see the toString section). It describes exactly when the numbers will switch to scientific notation when printed.

You can also look into NumberFormat.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜