在Java ME中将double加倍到5位小数 [英] Rounding a double to 5 decimal places in Java ME

查看:126
本文介绍了在Java ME中将double加倍到5位小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用 DecimalFormat 的情况下将双精度数转换为5位小数?

How do I round a double to 5 decimal places, without using DecimalFormat?

推荐答案

您可以通过乘以您的数字将其作为第一个小数位来舍入到小数点后五位。然后进行正常舍入,再次将其作为小数点后第五位。

You can round to the fifth decimal place by making it the first decimal place by multiplying your number. Then do normal rounding, and make it the fifth decimal place again.

假设圆的值为 double 名为 x

double factor = 1e5; // = 1 * 10^5 = 100000.
double result = Math.round(x * factor) / factor;

如果你想要舍入到6位小数,请让 factor 1e6 ,依此类推。

If you want to round to 6 decimal places, let factor be 1e6, and so on.

这篇关于在Java ME中将double加倍到5位小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆