四舍五入,同时保持尾随零 [英] Round off a double while maintaining the trailing zero

查看:79
本文介绍了四舍五入,同时保持尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的函数,用于将数字四舍五入到小数点后两位,但是当四舍五入后的数字为1.50时,它似乎忽略了结尾的零并仅返回1.5

Here is my function to roundoff a number upto two decimals but when the rounded off number is 1.50 it seems to ignore the trailing zero and just returns 1.5

public static double roundOff(double number) {
        double accuracy = 20;
        number = number * accuracy;
        number = Math.ceil(number);
        number = number / accuracy;
        return number;
    }

因此,如果我发送1.499,它将返回1.5,而我需要的是1.50

So if I send 1.499 it returns 1.5 where as I want 1.50

推荐答案

这是一个打印问题:

double d = 1.5;
System.out.println(String.format("%.2f", d)); // 1.50

这篇关于四舍五入,同时保持尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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