Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
like image 432
Rakesh Garia Avatar asked Feb 10 '23 12:02

Rakesh Garia


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

like image 78
dly Avatar answered Feb 12 '23 01:02

dly


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
like image 35
Pham Trung Avatar answered Feb 12 '23 02:02

Pham Trung