Skip to content

Commit 1a06dfb

Browse files
authored
Floating-point Formatting (eugenp#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
1 parent 9dbdfcf commit 1a06dfb

3 files changed

Lines changed: 195 additions & 29 deletions

File tree

‎core-java/src/main/java/com/baeldung/decimalformat/DoubletoString.java‎

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.baeldung.removingdecimals;
2+
3+
import org.junit.Test;
4+
import org.openjdk.jmh.annotations.*;
5+
import org.openjdk.jmh.runner.Runner;
6+
import org.openjdk.jmh.runner.options.Options;
7+
import org.openjdk.jmh.runner.options.OptionsBuilder;
8+
9+
import java.math.BigDecimal;
10+
import java.math.RoundingMode;
11+
import java.text.DecimalFormat;
12+
import java.text.NumberFormat;
13+
import java.util.concurrent.TimeUnit;
14+
15+
/**
16+
* This benchmark compares some of the approaches to formatting a floating-point
17+
* value into a {@link String} while removing the decimal part.
18+
*
19+
* To run, simply run the {@link RemovingDecimalsManualTest#runBenchmarks()} test
20+
* at the end of this class.
21+
*
22+
* The benchmark takes about 15 minutes to run. Since it is using {@link Mode#Throughput},
23+
* higher numbers mean better performance.
24+
*/
25+
@BenchmarkMode(Mode.Throughput)
26+
@Warmup(iterations = 5)
27+
@Measurement(iterations = 20)
28+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
29+
@State(Scope.Benchmark)
30+
public class RemovingDecimalsManualTest {
31+
@Param(value = {"345.56", "345345345.56", "345345345345345345.56"}) double doubleValue;
32+
33+
NumberFormat nf;
34+
DecimalFormat df;
35+
36+
@Setup
37+
public void readyFormatters() {
38+
nf = NumberFormat.getInstance();
39+
nf.setMaximumFractionDigits(0);
40+
df = new DecimalFormat("#,###");
41+
}
42+
43+
@Benchmark
44+
public String whenCastToInt_thenValueIsTruncated() {
45+
return String.valueOf((int) doubleValue);
46+
}
47+
48+
@Benchmark
49+
public String whenUsingStringFormat_thenValueIsRounded() {
50+
return String.format("%.0f", doubleValue);
51+
}
52+
53+
@Benchmark
54+
public String whenUsingNumberFormat_thenValueIsRounded() {
55+
nf.setRoundingMode(RoundingMode.HALF_UP);
56+
return nf.format(doubleValue);
57+
}
58+
59+
@Benchmark
60+
public String whenUsingNumberFormatWithFloor_thenValueIsTruncated() {
61+
nf.setRoundingMode(RoundingMode.FLOOR);
62+
return nf.format(doubleValue);
63+
}
64+
65+
@Benchmark
66+
public String whenUsingDecimalFormat_thenValueIsRounded() {
67+
df.setRoundingMode(RoundingMode.HALF_UP);
68+
return df.format(doubleValue);
69+
}
70+
71+
@Benchmark
72+
public String whenUsingDecimalFormatWithFloor_thenValueIsTruncated() {
73+
df.setRoundingMode(RoundingMode.FLOOR);
74+
return df.format(doubleValue);
75+
}
76+
77+
@Benchmark
78+
public String whenUsingBigDecimalDoubleValue_thenValueIsTruncated() {
79+
BigDecimal big = new BigDecimal(doubleValue);
80+
big = big.setScale(0, RoundingMode.FLOOR);
81+
return big.toString();
82+
}
83+
84+
@Benchmark
85+
public String whenUsingBigDecimalDoubleValueWithHalfUp_thenValueIsRounded() {
86+
BigDecimal big = new BigDecimal(doubleValue);
87+
big = big.setScale(0, RoundingMode.HALF_UP);
88+
return big.toString();
89+
}
90+
91+
@Test
92+
public void runBenchmarks() throws Exception {
93+
Options options = new OptionsBuilder()
94+
.include(this.getClass().getSimpleName()).threads(1)
95+
.forks(1).shouldFailOnError(true).shouldDoGC(true)
96+
.jvmArgs("-server").build();
97+
98+
new Runner(options).run();
99+
}
100+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.baeldung.removingdecimals;
2+
3+
import org.junit.Test;
4+
5+
import java.math.BigDecimal;
6+
import java.math.RoundingMode;
7+
import java.text.DecimalFormat;
8+
import java.text.NumberFormat;
9+
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertNotEquals;
12+
13+
/**
14+
* Tests that demonstrate some different approaches for formatting a
15+
* floating-point value into a {@link String} while removing the decimal part.
16+
*/
17+
public class RemovingDecimalsUnitTest {
18+
private final double doubleValue = 345.56;
19+
20+
@Test
21+
public void whenCastToInt_thenValueIsTruncated() {
22+
String truncated = String.valueOf((int) doubleValue);
23+
assertEquals("345", truncated);
24+
}
25+
26+
@Test
27+
public void givenALargeDouble_whenCastToInt_thenValueIsNotTruncated() {
28+
double outOfIntRange = 6_000_000_000.56;
29+
String truncationAttempt = String.valueOf((int) outOfIntRange);
30+
assertNotEquals("6000000000", truncationAttempt);
31+
}
32+
33+
@Test
34+
public void whenUsingStringFormat_thenValueIsRounded() {
35+
String rounded = String.format("%.0f", doubleValue);
36+
assertEquals("346", rounded);
37+
}
38+
39+
@Test
40+
public void givenALargeDouble_whenUsingStringFormat_thenValueIsStillRounded() {
41+
double outOfIntRange = 6_000_000_000.56;
42+
String rounded = String.format("%.0f", outOfIntRange);
43+
assertEquals("6000000001", rounded);
44+
}
45+
46+
@Test
47+
public void whenUsingNumberFormat_thenValueIsRounded() {
48+
NumberFormat nf = NumberFormat.getInstance();
49+
nf.setMaximumFractionDigits(0);
50+
nf.setRoundingMode(RoundingMode.HALF_UP);
51+
String rounded = nf.format(doubleValue);
52+
assertEquals("346", rounded);
53+
}
54+
55+
@Test
56+
public void whenUsingNumberFormatWithFloor_thenValueIsTruncated() {
57+
NumberFormat nf = NumberFormat.getInstance();
58+
nf.setMaximumFractionDigits(0);
59+
nf.setRoundingMode(RoundingMode.FLOOR);
60+
String truncated = nf.format(doubleValue);
61+
assertEquals("345", truncated);
62+
}
63+
64+
@Test
65+
public void whenUsingDecimalFormat_thenValueIsRounded() {
66+
DecimalFormat df = new DecimalFormat("#,###");
67+
df.setRoundingMode(RoundingMode.HALF_UP);
68+
String rounded = df.format(doubleValue);
69+
assertEquals("346", rounded);
70+
}
71+
72+
@Test
73+
public void whenUsingDecimalFormatWithFloor_thenValueIsTruncated() {
74+
DecimalFormat df = new DecimalFormat("#,###");
75+
df.setRoundingMode(RoundingMode.FLOOR);
76+
String truncated = df.format(doubleValue);
77+
assertEquals("345", truncated);
78+
}
79+
80+
@Test
81+
public void whenUsingBigDecimalDoubleValue_thenValueIsTruncated() {
82+
BigDecimal big = new BigDecimal(doubleValue);
83+
big = big.setScale(0, RoundingMode.FLOOR);
84+
String truncated = big.toString();
85+
assertEquals("345", truncated);
86+
}
87+
88+
@Test
89+
public void whenUsingBigDecimalDoubleValueWithHalfUp_thenValueIsRounded() {
90+
BigDecimal big = new BigDecimal(doubleValue);
91+
big = big.setScale(0, RoundingMode.HALF_UP);
92+
String truncated = big.toString();
93+
assertEquals("346", truncated);
94+
}
95+
}

0 commit comments

Comments
 (0)