简体   繁体   中英

How can I format a String number without round off?

The requirement for me is that user should be able to enter any valid number from a form (the number will be in a string). The application should format the String number with commas in US format without any round-off. This formatted number will be printed in a letter that will be mailed to customers. Most Number formatters will round off the number after a specified amount of precision. This is required in Java but any algorithm in any language will do. I can code that to Java.

Example:

Input               Output 
-1.0                -1.0
123                 123
1234                1,234
123456              123,456
-123456             -123,456
-123456.01          -123,456.01
1234.79999999       1,234.79999999
1234567.79999999    1,234,567.79999999
-1234567.79999999   -1,234,567.79999999

There is a problem with using DecimalFormat as answered in this question: Add commas (grouping separator) to number without modifying decimals?

If you define it as below, then you are limited to 2 decimal places. If you increase the number of zeroes in the format pattern, then the decimal places also increase but are fixed. Also, if you give input as 2.1, you will get 2.10. I need 2.1. And if you give an input of 11220.999, then the result will be 11,221.00. I need exactly 11220.999.

DecimalFormat decimalFormat = new DecimalFormat("#.00");
    String number = "-123456.799999";
    double realnumber = Double.parseDouble(number);
    NumberFormat nf = NumberFormat.getInstance(Locale.US);
    nf.setMaximumFractionDigits(10);  
    System.out.printf("%s", nf.format(realnumber));
    // System.out.println(nf.format(realnumber)); // both work

Result -123,456.799999

Explanation: nf.setMaximumFractionDigits(n); Set this to the highest amount of digits you want to display. All numbers with this number of digits or less after the comma will be displayed like you want them to. Only numbers with more digits after the comma will be rounded.

Edit for BIG numbers using String concat

String number = "-12342342353356.799999123456789";
int startIndex = number.length()-3;
if(number.indexOf(".") >= 0) {
    startIndex = number.indexOf(".") - 3;
}
int negative = (number.charAt(0) == '-' ? 1 : 0);
for (int i = startIndex; i > 0 + negative; i -= 3) {
    number = number.substring(0, i) + "," + number.substring(i);
}
System.out.println(number);

Result: -12,342,342,353,356.799999123456789

What you need is:

NumberFormat format = NumberFormat.getNumberInstance(Locale.US);
format.setMaximumFractionDigits(Integer.MAX_VALUE);
System.out.println(format.format(Double.valueOf("2.10")));
System.out.println(format.format(Double.valueOf("11220.999")));
System.out.println(format.format(Double.valueOf("-1234567.79999999")));

Output:

2.1
11,220.999
-1,234,567.79999999
-999,999,999.999999

For bigger string, you may need to use BigDecimal

BigDecimal de = new BigDecimal("-999999999.99999999999999999999");
System.out.println(format.format(de));

Output

-999,999,999.99999999999999999999
    double number = -999999999.999999;

    NumberFormat nf = NumberFormat.getNumberInstance();
    nf.setMaximumFractionDigits(10);
    nf.setRoundingMode(RoundingMode.FLOOR);

    System.out.println(nf.format(number));

Result:

-999,999,999.999999

another way to deal with very long numbers as String

    String number = "999999999999999.9999999999998881928391283";

    BigDecimal bd = new BigDecimal(number);

    NumberFormat format = NumberFormat.getNumberInstance();

    StringBuilder sb = new StringBuilder();
    sb.append(format.format(bd.longValue()));

    int index = number.indexOf('.');
    if (index > -1) {
        sb.append(number.substring(index));
    }
    System.out.println(sb);

Result:

999,999,999,999,999.9999999999998881928391283

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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