Floating-point Formatting (#5244)

These samples show some different approaches for formatting a
floating-point value into a String, while removing its decimal part.

Included is a benchmark to show how each approach performs.

Issue: BAEL-2152
This commit is contained in:
Josh Cummings
2018-09-13 06:42:33 -06:00
committed by GitHub
parent 9dbdfcf31f
commit 1a06dfb941
3 changed files with 195 additions and 29 deletions
@@ -1,29 +0,0 @@
package com.baeldung.decimalformat;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class DoubletoString {
public static void main(String[] args) {
double doubleValue = 345.56;
System.out.println(String.valueOf((int) doubleValue));
System.out.println(String.format("%.0f", doubleValue));
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(0);
nf.setRoundingMode(RoundingMode.FLOOR);
System.out.println(nf.format(doubleValue));
doubleValue = Math.floor(doubleValue);
DecimalFormat df = new DecimalFormat("#,###");
df.setRoundingMode(RoundingMode.FLOOR);
System.out.println(df.format(doubleValue));
}
}