Last active
March 9, 2023 11:10
-
-
Save adojos/a7ae5d56e40b417c0886184471c0f0ca to your computer and use it in GitHub Desktop.
Java: Format Decimal Set Max/Min Int & Fraction Digits #java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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