How to add comma to a number every 3 digits in Java

2 Answers

0 votes
import java.text.DecimalFormat;

public class MyClass {
    public static void main(String args[]) {
        double f = 8494008356.07;
        
        DecimalFormat decimalFormat = new DecimalFormat("#.##");
        decimalFormat.setGroupingUsed(true);
        decimalFormat.setGroupingSize(3);
  
        System.out.println(decimalFormat.format(f));
    }
}


 
 
/*
run:
 
8,494,008,356.07
 
*/

 



answered Nov 5, 2021 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        double f = 8494008356.07;
        
        System.out.format("%,.2f\n", f);    
        System.out.format("%+,.2f", f); 
    }
}


  
  
/*
run:
  
8,494,008,356.07
+8,494,008,356.07

*/

 



answered Mar 30, 2022 by avibootz

Related questions

1 answer 119 views
1 answer 136 views
1 answer 134 views
1 answer 137 views
2 answers 144 views
1 answer 121 views
1 answer 111 views
...