How can I format the line number without rounding?

The requirement for me is that the user should be able to enter any valid number from the form (the number will be on the line). The application must format the line number with commas in US format without rounding. This formatted number will be printed in the letter that will be sent to customers. Most number formatters round a number after a certain amount of precision. This is required in Java, but any algorithm in any language will work. I can code this in 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 using DecimalFormat as the answer to this question: Add commas (grouping separator) to number without changing decimal places?

If you define it as shown below, you are limited to two decimal places. If you increase the number of zeros in the format pattern, then the decimal places also increase, but are fixed. Also, if you specify the input as 2.1, you get 2.10. I need 2.1. And if you give the input 11220.999, then the result is 11,221.00. I need exactly 11220.999.

DecimalFormat decimalFormat = new DecimalFormat("#.00");

      

+3


source to share


3 answers


    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 maximum number of digits you want to display. All numbers with as many digits or less after the decimal point will be displayed the way you want. Only numbers with more decimal places will be rounded.



Change for large 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

+4


source


What you need:

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 a larger string, you may need BigDecimal

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

      

Output

-999,999,999.99999999999999999999

      

+2


source


    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 like 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

      

+1


source







All Articles