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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 195 additions and 29 deletions

View File

@ -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));
}
}

View File

@ -0,0 +1,100 @@
package com.baeldung.removingdecimals;
import org.junit.Test;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.concurrent.TimeUnit;
/**
* This benchmark compares some of the approaches to formatting a floating-point
* value into a {@link String} while removing the decimal part.
*
* To run, simply run the {@link RemovingDecimalsManualTest#runBenchmarks()} test
* at the end of this class.
*
* The benchmark takes about 15 minutes to run. Since it is using {@link Mode#Throughput},
* higher numbers mean better performance.
*/
@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 5)
@Measurement(iterations = 20)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
public class RemovingDecimalsManualTest {
@Param(value = {"345.56", "345345345.56", "345345345345345345.56"}) double doubleValue;
NumberFormat nf;
DecimalFormat df;
@Setup
public void readyFormatters() {
nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(0);
df = new DecimalFormat("#,###");
}
@Benchmark
public String whenCastToInt_thenValueIsTruncated() {
return String.valueOf((int) doubleValue);
}
@Benchmark
public String whenUsingStringFormat_thenValueIsRounded() {
return String.format("%.0f", doubleValue);
}
@Benchmark
public String whenUsingNumberFormat_thenValueIsRounded() {
nf.setRoundingMode(RoundingMode.HALF_UP);
return nf.format(doubleValue);
}
@Benchmark
public String whenUsingNumberFormatWithFloor_thenValueIsTruncated() {
nf.setRoundingMode(RoundingMode.FLOOR);
return nf.format(doubleValue);
}
@Benchmark
public String whenUsingDecimalFormat_thenValueIsRounded() {
df.setRoundingMode(RoundingMode.HALF_UP);
return df.format(doubleValue);
}
@Benchmark
public String whenUsingDecimalFormatWithFloor_thenValueIsTruncated() {
df.setRoundingMode(RoundingMode.FLOOR);
return df.format(doubleValue);
}
@Benchmark
public String whenUsingBigDecimalDoubleValue_thenValueIsTruncated() {
BigDecimal big = new BigDecimal(doubleValue);
big = big.setScale(0, RoundingMode.FLOOR);
return big.toString();
}
@Benchmark
public String whenUsingBigDecimalDoubleValueWithHalfUp_thenValueIsRounded() {
BigDecimal big = new BigDecimal(doubleValue);
big = big.setScale(0, RoundingMode.HALF_UP);
return big.toString();
}
@Test
public void runBenchmarks() throws Exception {
Options options = new OptionsBuilder()
.include(this.getClass().getSimpleName()).threads(1)
.forks(1).shouldFailOnError(true).shouldDoGC(true)
.jvmArgs("-server").build();
new Runner(options).run();
}
}

View File

@ -0,0 +1,95 @@
package com.baeldung.removingdecimals;
import org.junit.Test;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
/**
* Tests that demonstrate some different approaches for formatting a
* floating-point value into a {@link String} while removing the decimal part.
*/
public class RemovingDecimalsUnitTest {
private final double doubleValue = 345.56;
@Test
public void whenCastToInt_thenValueIsTruncated() {
String truncated = String.valueOf((int) doubleValue);
assertEquals("345", truncated);
}
@Test
public void givenALargeDouble_whenCastToInt_thenValueIsNotTruncated() {
double outOfIntRange = 6_000_000_000.56;
String truncationAttempt = String.valueOf((int) outOfIntRange);
assertNotEquals("6000000000", truncationAttempt);
}
@Test
public void whenUsingStringFormat_thenValueIsRounded() {
String rounded = String.format("%.0f", doubleValue);
assertEquals("346", rounded);
}
@Test
public void givenALargeDouble_whenUsingStringFormat_thenValueIsStillRounded() {
double outOfIntRange = 6_000_000_000.56;
String rounded = String.format("%.0f", outOfIntRange);
assertEquals("6000000001", rounded);
}
@Test
public void whenUsingNumberFormat_thenValueIsRounded() {
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(0);
nf.setRoundingMode(RoundingMode.HALF_UP);
String rounded = nf.format(doubleValue);
assertEquals("346", rounded);
}
@Test
public void whenUsingNumberFormatWithFloor_thenValueIsTruncated() {
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(0);
nf.setRoundingMode(RoundingMode.FLOOR);
String truncated = nf.format(doubleValue);
assertEquals("345", truncated);
}
@Test
public void whenUsingDecimalFormat_thenValueIsRounded() {
DecimalFormat df = new DecimalFormat("#,###");
df.setRoundingMode(RoundingMode.HALF_UP);
String rounded = df.format(doubleValue);
assertEquals("346", rounded);
}
@Test
public void whenUsingDecimalFormatWithFloor_thenValueIsTruncated() {
DecimalFormat df = new DecimalFormat("#,###");
df.setRoundingMode(RoundingMode.FLOOR);
String truncated = df.format(doubleValue);
assertEquals("345", truncated);
}
@Test
public void whenUsingBigDecimalDoubleValue_thenValueIsTruncated() {
BigDecimal big = new BigDecimal(doubleValue);
big = big.setScale(0, RoundingMode.FLOOR);
String truncated = big.toString();
assertEquals("345", truncated);
}
@Test
public void whenUsingBigDecimalDoubleValueWithHalfUp_thenValueIsRounded() {
BigDecimal big = new BigDecimal(doubleValue);
big = big.setScale(0, RoundingMode.HALF_UP);
String truncated = big.toString();
assertEquals("346", truncated);
}
}