Skip to content

Instantly share code, notes, and snippets.

@adojos
Last active March 9, 2023 11:10
Show Gist options
  • Save adojos/a7ae5d56e40b417c0886184471c0f0ca to your computer and use it in GitHub Desktop.
Save adojos/a7ae5d56e40b417c0886184471c0f0ca to your computer and use it in GitHub Desktop.
Java: Format Decimal Set Max/Min Int & Fraction Digits #java
import java.text.DecimalFormat;
public class DecimalFormatSetMinMaxDigit {
public static void main(String[] args) {
double dbNum = 170180.24551D;
DecimalFormat deciFormat = new DecimalFormat();
System.out.println("Original Num: " + dbNum);
String strFormattedNum = deciFormat.format(dbNum);
System.out.println("JVM Locale Formatted Num: " + strFormattedNum);
String strPattern = "#,####.####";
deciFormat.applyPattern(strPattern);
strFormattedNum = deciFormat.format(dbNum);
System.out.println("Pattern Formatted Num: " + strFormattedNum);
deciFormat.setMinimumFractionDigits(1);
deciFormat.setMaximumFractionDigits(4);
strFormattedNum = deciFormat.format(dbNum);
System.out.println("With Min/Max Fraction Digits: " + strFormattedNum);
deciFormat.setMinimumIntegerDigits(1);
deciFormat.setMaximumIntegerDigits(3);
strFormattedNum = deciFormat.format(dbNum);
System.out.println("With Min/Max Integer & Fraction Digits: " + strFormattedNum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment