Created
December 30, 2021 16:38
-
-
Save adojos/032765732f6e797548dcab2d55feefd6 to your computer and use it in GitHub Desktop.
Java: Format Decimal using Rounding #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.math.RoundingMode; | |
import java.text.DecimalFormat; | |
public class DecimalFormatRounding { | |
public static void main(String[] args) { | |
double dbNum = 170180.24551D; | |
DecimalFormat dFormat = new DecimalFormat(); | |
dFormat.setRoundingMode(RoundingMode.HALF_UP); | |
String strFormattedNum = dFormat.format(dbNum); | |
System.out.println(dbNum + " Rounded-UP " + strFormattedNum); | |
dFormat.setRoundingMode(RoundingMode.DOWN); | |
strFormattedNum = dFormat.format(dbNum); | |
System.out.println(dbNum + " Rounded-DOWN " + strFormattedNum); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment