[BAEL-11403] - Moved articles out of core-java (part 4)
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
package com.baeldung.decimalformat;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.DecimalFormatSymbols;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.ParseException;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class DecimalFormatExamplesUnitTest {
|
||||
|
||||
double d = 1234567.89;
|
||||
|
||||
@Test
|
||||
public void givenSimpleDecimal_WhenFormatting_ThenCorrectOutput() {
|
||||
|
||||
assertThat(new DecimalFormat("#.##", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
|
||||
.isEqualTo("1234567.89");
|
||||
|
||||
assertThat(new DecimalFormat("0.00", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
|
||||
.isEqualTo("1234567.89");
|
||||
|
||||
assertThat(new DecimalFormat("#########.###", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
|
||||
.isEqualTo("1234567.89");
|
||||
|
||||
assertThat(new DecimalFormat("000000000.000", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
|
||||
.isEqualTo("001234567.890");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSmallerDecimalPattern_WhenFormatting_ThenRounding() {
|
||||
|
||||
assertThat(new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)).isEqualTo("1234567.9");
|
||||
|
||||
assertThat(new DecimalFormat("#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)).isEqualTo("1234568");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGroupingSeparator_WhenFormatting_ThenGroupedOutput() {
|
||||
|
||||
assertThat(new DecimalFormat("#,###.#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
|
||||
.isEqualTo("1,234,567.9");
|
||||
|
||||
assertThat(new DecimalFormat("#,###", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
|
||||
.isEqualTo("1,234,568");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMixedPattern_WhenFormatting_ThenCorrectOutput() {
|
||||
|
||||
assertThat(new DecimalFormat("The # number", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
|
||||
.isEqualTo("The 1234568 number");
|
||||
|
||||
assertThat(new DecimalFormat("The '#' # number", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
|
||||
.isEqualTo("The # 1234568 number");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocales_WhenFormatting_ThenCorrectOutput() {
|
||||
|
||||
assertThat(new DecimalFormat("#,###.##", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
|
||||
.isEqualTo("1,234,567.89");
|
||||
|
||||
assertThat(new DecimalFormat("#,###.##", new DecimalFormatSymbols(Locale.ITALIAN)).format(d))
|
||||
.isEqualTo("1.234.567,89");
|
||||
|
||||
assertThat(new DecimalFormat("#,###.##", DecimalFormatSymbols.getInstance(new Locale("it", "IT"))).format(d))
|
||||
.isEqualTo("1.234.567,89");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenE_WhenFormatting_ThenScientificNotation() {
|
||||
|
||||
assertThat(new DecimalFormat("00.#######E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
|
||||
.isEqualTo("12.3456789E5");
|
||||
|
||||
assertThat(new DecimalFormat("000.000000E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
|
||||
.isEqualTo("123.456789E4");
|
||||
|
||||
assertThat(new DecimalFormat("##0.######E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
|
||||
.isEqualTo("1.23456789E6");
|
||||
|
||||
assertThat(new DecimalFormat("###.000000E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
|
||||
.isEqualTo("1.23456789E6");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_WhenParsing_ThenCorrectOutput() throws ParseException {
|
||||
|
||||
assertThat(new DecimalFormat("", new DecimalFormatSymbols(Locale.ENGLISH)).parse("1234567.89"))
|
||||
.isEqualTo(1234567.89);
|
||||
|
||||
assertThat(new DecimalFormat("", new DecimalFormatSymbols(Locale.ITALIAN)).parse("1.234.567,89"))
|
||||
.isEqualTo(1234567.89);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringAndBigDecimalFlag_WhenParsing_ThenCorrectOutput() throws ParseException {
|
||||
|
||||
NumberFormat nf = new DecimalFormat("", new DecimalFormatSymbols(Locale.ENGLISH));
|
||||
((DecimalFormat) nf).setParseBigDecimal(true);
|
||||
assertThat(nf.parse("1234567.89")).isEqualTo(BigDecimal.valueOf(1234567.89));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
package com.baeldung.java.math;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class MathUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenAbsInteger_thenReturnAbsoluteValue() {
|
||||
assertEquals(5,Math.abs(-5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMaxBetweenTwoValue_thenReturnMaximum() {
|
||||
assertEquals(10, Math.max(5,10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMinBetweenTwoValue_thenReturnMinimum() {
|
||||
assertEquals(5, Math.min(5,10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSignumWithNegativeNumber_thenReturnMinusOne() {
|
||||
assertEquals(-1, Math.signum(-5), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCopySignWithNegativeSign_thenReturnNegativeArgument() {
|
||||
assertEquals(-5, Math.copySign(5,-1), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPow_thenReturnPoweredValue() {
|
||||
assertEquals(25, Math.pow(5,2),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSqrt_thenReturnSquareRoot() {
|
||||
assertEquals(5, Math.sqrt(25),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCbrt_thenReturnCubeRoot() {
|
||||
assertEquals(5, Math.cbrt(125),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenExp_thenReturnEulerNumberRaised() {
|
||||
assertEquals(2.718, Math.exp(1),0.1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenExpm1_thenReturnEulerNumberMinusOne() {
|
||||
assertEquals(1.718, Math.expm1(1),0.1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetExponent_thenReturnUnbiasedExponent() {
|
||||
assertEquals(8, Math.getExponent(333.3),0);
|
||||
assertEquals(7, Math.getExponent(222.2f),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLog_thenReturnValue() {
|
||||
assertEquals(1, Math.log(Math.E),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLog10_thenReturnValue() {
|
||||
assertEquals(1, Math.log10(10),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLog1p_thenReturnValue() {
|
||||
assertEquals(1.31, Math.log1p(Math.E),0.1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSin_thenReturnValue() {
|
||||
assertEquals(1, Math.sin(Math.PI/2),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCos_thenReturnValue() {
|
||||
assertEquals(1, Math.cos(0),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTan_thenReturnValue() {
|
||||
assertEquals(1, Math.tan(Math.PI/4),0.1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAsin_thenReturnValue() {
|
||||
assertEquals(Math.PI/2, Math.asin(1),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAcos_thenReturnValue() {
|
||||
assertEquals(Math.PI/2, Math.acos(0),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAtan_thenReturnValue() {
|
||||
assertEquals(Math.PI/4, Math.atan(1),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAtan2_thenReturnValue() {
|
||||
assertEquals(Math.PI/4, Math.atan2(1,1),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenToDegrees_thenReturnValue() {
|
||||
assertEquals(180, Math.toDegrees(Math.PI),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenToRadians_thenReturnValue() {
|
||||
assertEquals(Math.PI, Math.toRadians(180),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCeil_thenReturnValue() {
|
||||
assertEquals(4, Math.ceil(Math.PI),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFloor_thenReturnValue() {
|
||||
assertEquals(3, Math.floor(Math.PI),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetExponent_thenReturnValue() {
|
||||
assertEquals(8, Math.getExponent(333.3),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIEEERemainder_thenReturnValue() {
|
||||
assertEquals(1.0, Math.IEEEremainder(5,2),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNextAfter_thenReturnValue() {
|
||||
assertEquals(1.9499999284744263, Math.nextAfter(1.95f,1),0.0000001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNextUp_thenReturnValue() {
|
||||
assertEquals(1.9500002, Math.nextUp(1.95f),0.0000001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRint_thenReturnValue() {
|
||||
assertEquals(2.0, Math.rint(1.95f),0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRound_thenReturnValue() {
|
||||
assertEquals(2.0, Math.round(1.95f),0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenScalb_thenReturnValue() {
|
||||
assertEquals(48, Math.scalb(3, 4),0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenHypot_thenReturnValue() {
|
||||
assertEquals(5, Math.hypot(4, 3),0.0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.nth.root.calculator;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
public class NthRootCalculatorUnitTest {
|
||||
|
||||
private NthRootCalculator nthRootCalculator = new NthRootCalculator();
|
||||
|
||||
@Test
|
||||
public void whenBaseIs125AndNIs3_thenNthRootIs5() {
|
||||
Double result = nthRootCalculator.calculate(125.0, 3.0);
|
||||
assertEquals(result, (Double) 5.0d);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.nth.root.main;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class MainUnitTest {
|
||||
@InjectMocks
|
||||
private Main main;
|
||||
|
||||
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
||||
private final PrintStream originalOut = System.out;
|
||||
|
||||
@Before
|
||||
public void setUpStreams() {
|
||||
System.setOut(new PrintStream(outContent));
|
||||
}
|
||||
|
||||
@After
|
||||
public void restoreStreams() {
|
||||
System.setOut(originalOut);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenThatTheBaseIs125_andTheExpIs3_whenMainIsCalled_thenTheCorrectResultIsPrinted() {
|
||||
main.main(new String[]{"125.0", "3.0"});
|
||||
assertEquals("The 3.0 root of 125.0 equals to 5.0.\n", outContent.toString().replaceAll("\r", ""));
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user