How to format Decimal Number in Java - DecimalFormat Example
We often need to format decimal numbers in Java like formatting numbers up to 2 decimal places or 3 decimal places or we want to introduce leading zeros in front of numbers. Thankfully Java programming language provides many different ways to format numbers in Java like either using Math.round() or setScale() from BigDecimal but the caveat is that they also do rounding of numbers i.e. 1.6 will be rounded on 2.0 if we use Match.round(). If we are just interested in formatting decimal numbers up to n decimal digits then DecimalFormat is the way to go. java.text.DecimalFormat descends from NumberFormat and provides a dynamic way of formatting numbers in Java.
تعليقات
إرسال تعليق