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:
@@ -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));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user