DecimalFormat
Using java.text.DecimalFormat class you can control the display of leading and trailing zeros, prefixes and suffixes, thousand separators, and the decimal separator.
Example:
Using java.text.DecimalFormat class you can control the display of leading and trailing zeros, prefixes and suffixes, thousand separators, and the decimal separator.
Example:
package com.selenium;
import java.text.DecimalFormat;
public class DecimalFormatEx {
public static void main(String[] args) {
/* first parameter - pattern second parameter - value
* According to the given pattern
*/
//acts as a thousand separator - 12,345.343
formatByPattern("###,###.###",12345.34343);
//here the decimal value is rounding - 123456.79
formatByPattern("###.##",123456.789);
//adding trailing and leading zeros - 000898.780
formatByPattern("000000.000",898.78);
//adding prefix $ - $12,345.34
formatByPattern("$###,###.###",12345.34);
}
//a custom method - you can name this method as you wish
private static void formatByPattern(String pattern,double value){
DecimalFormat formater = new DecimalFormat(pattern);
String output = formater.format(value);
System.out.println(output);
}
}
No comments:
Post a Comment