在Java中最多2位小数?

编程入门 行业动态 更新时间:2024-10-24 15:25:26
本文介绍了在Java中最多2位小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经阅读了很多stackoverflow问题,但似乎没有一个对我有用。我正在使用 math.round()进行舍入。 这是代码:

I have read a lot of stackoverflow questions but none seems to be working for me. i am using math.round() to round off. this is the code:

class round{ public static void main(String args[]){ double a = 123.13698; double roundOff = Math.round(a*100)/100; System.out.println(roundOff); } }

我得到的输出是: 123 但我希望它是 123.14 。我读到添加 * 100/100 会有所帮助,但你可以看到我没有设法让它工作。

the output i get is: 123 but i want it to be 123.14. i read that adding *100/100 will help but as you can see i didn't manage to get it to work.

输入和输出都必须是双倍的。

it is absolutely essential for both input and output to be a double.

如果你改变上面代码的第4行,那将是非常有用的。发布它。

it would be great great help if you change the line 4 of the code above and post it.

推荐答案

这个有效......

double roundOff = Math.round(a * 100.0) / 100.0;

输出

123.14

或者@Rufein说

double roundOff = (double) Math.round(a * 100) / 100;

这也适合你。

更多推荐

在Java中最多2位小数?

本文发布于:2023-11-10 06:15:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574577.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最多   小数   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!