简体   繁体   English

如何使用DecimalFormat方法更改小数点前的数字大小

[英]how to change the size of a number before the decimal point with DecimalFormat method

how to limit the number of digits of the whole number itself not just the fractional part of the number after the decimal? 如何限制整数本身的位数,而不仅限于小数点后数字的小数部分?

for example using the number 54321.12574857f with the method DecimalFormat("##0.##"); 例如,使用数字54321.12574857f和DecimalFormat(“ ## 0。##”)方法; it should return 543.12 or 321.12, the two decimal places after the decimal are correct however I want to reduce the numbers before the decimal to only 3. how is this done? 它应该返回543.12或321.12,小数点后的两个小数位是正确的,但是我想将小数点前的数字减少到仅3。这是怎么做的?

after entering the number 54321.12574857 I was using the parameter for the DecimalFormat method of ##0.## and I was expecting 543.12, or 321.12 not what I am getting which is 54321.12 输入数字54321.12574857后,我正在使用## 0。##的DecimalFormat方法的参数,但我期望的是543.12或321.12,而不是得到的54321.12

the only numbers changed by DecimalFormat method are those after the decimal 由DecimalFormat方法更改的唯一数字是小数点后的数字

            float inputNumber = 54321.12574857f;

    DecimalFormat decimalFormat = new DecimalFormat("##0.##");
    String newNumber = String.valueOf(decimalFormat.format(inputNumber));

    System.out.println(newNumber);

Try some thing like this, 试试这样的事

    DecimalFormat decimalFormat = new DecimalFormat("##0.##");
    String newNumber = String.valueOf(decimalFormat.format(inputNumber/100));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM