我是靠谱客的博主 谨慎冥王星,最近开发中收集的这篇文章主要介绍java 声明 float 小数,我需要在Java中将float浮点到两位小数,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

I am having difficulties rounding a float to two decimal places. I have tried a few methods I have seen on here including simply just using Math.round(), but no matter what I do I keep getting unusual numbers.

I have a list of floats that I am processing, the first in the list is displayed as 1.2975118E7. What is the E7?

When I use Math.round(f) (f is the float), I get the exact same number.

I know I am doing something wrong, I just am not sure what.

I just want the numbers to be in the format x.xx. The first number should be 1.30, etc.

解决方案

1.2975118E7 = 1.2975118 * 10^7 = 12975118

Also, Math.round(f) returns an integer. You can't use it to get your desired format x.xx.

You could use String.format.

String s = String.format("%.2f", 1.2975118);

// 1.30

最后

以上就是谨慎冥王星为你收集整理的java 声明 float 小数,我需要在Java中将float浮点到两位小数的全部内容,希望文章能够帮你解决java 声明 float 小数,我需要在Java中将float浮点到两位小数所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部