Revert "BAEL-4134"
This commit is contained in:
committed by
GitHub
parent
dffa1f64e6
commit
485b4e3e99
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.formatNumber;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.Locale;
|
||||
|
||||
public class FormatNumber {
|
||||
public static double withBigDecimal(double value, int places) {
|
||||
if (places < 0)
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
BigDecimal bigDecimal = new BigDecimal(value);
|
||||
bigDecimal = bigDecimal.setScale(places, RoundingMode.HALF_UP);
|
||||
return bigDecimal.doubleValue();
|
||||
}
|
||||
|
||||
public static double withMathRound(double value, int places) {
|
||||
double scale = Math.pow(10, places);
|
||||
return Math.round(value * scale) / scale;
|
||||
}
|
||||
|
||||
public static double withDecimalFormatPattern(double value, int places) {
|
||||
DecimalFormat df2 = new DecimalFormat("#,###,###,##0.00");
|
||||
DecimalFormat df3 = new DecimalFormat("#,###,###,##0.000");
|
||||
if (places == 2)
|
||||
return new Double(df2.format(value));
|
||||
else if (places == 3)
|
||||
return new Double(df3.format(value));
|
||||
else
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
public static double withDecimalFormatLocal(double value) {
|
||||
DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.getDefault());
|
||||
return new Double(df.format(value));
|
||||
}
|
||||
|
||||
public static String withStringFormat(double value, int places) {
|
||||
return String.format("%." + places + "f", value);
|
||||
}
|
||||
|
||||
public static String byPaddingZeros(int value, int paddingLength) {
|
||||
return String.format("%0" + paddingLength + "d", value);
|
||||
}
|
||||
|
||||
public static double withTwoDecimalPlaces(double value) {
|
||||
DecimalFormat df = new DecimalFormat("#.00");
|
||||
return new Double(df.format(value));
|
||||
}
|
||||
|
||||
public static String withLargeIntegers(double value) {
|
||||
DecimalFormat df = new DecimalFormat("###,###,###");
|
||||
return df.format(value);
|
||||
}
|
||||
|
||||
public static String forPercentages(double value, Locale localisation) {
|
||||
NumberFormat nf = NumberFormat.getPercentInstance(localisation);
|
||||
return nf.format(value);
|
||||
}
|
||||
|
||||
public static String currencyWithChosenLocalisation(double value, Locale localisation) {
|
||||
NumberFormat nf = NumberFormat.getCurrencyInstance(localisation);
|
||||
return nf.format(value);
|
||||
}
|
||||
|
||||
public static String currencyWithDefaultLocalisation(double value) {
|
||||
NumberFormat nf = NumberFormat.getCurrencyInstance();
|
||||
return nf.format(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.integerToBinary;
|
||||
|
||||
public class IntegerToBinary {
|
||||
public static String convertIntegerToBinary(int n) {
|
||||
if(n == 0) {
|
||||
return "0";
|
||||
}
|
||||
StringBuilder binaryNumber = new StringBuilder();
|
||||
while (n > 0) {
|
||||
int remainder = n % 2;
|
||||
binaryNumber.append(remainder);
|
||||
n /= 2;
|
||||
}
|
||||
binaryNumber = binaryNumber.reverse();
|
||||
return binaryNumber.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.baeldung.divisionbyzero;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class DivisionByZeroUnitTest {
|
||||
|
||||
@Test
|
||||
void givenInt_whenDividedByZero_thenThrowException() {
|
||||
assertThrows(ArithmeticException.class, () -> {
|
||||
int result = 12 / 0;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenDividingIntZeroByZero_thenThrowException() {
|
||||
assertThrows(ArithmeticException.class, () -> {
|
||||
int result = 0 / 0;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenDividingFloatingNumberByZero_thenNoExceptionIsThrown() {
|
||||
assertDoesNotThrow(() -> {
|
||||
float result = 0f / 0;
|
||||
});
|
||||
assertDoesNotThrow(() -> {
|
||||
double result = 0d / 0;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPositiveFloatingNumber_whenDividedByZero_thenReturnPositiveInfinity() {
|
||||
assertEquals(Float.POSITIVE_INFINITY, 12f / 0);
|
||||
assertEquals(Double.POSITIVE_INFINITY, 12d / 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNegativeFloatingNumber_whenDividedByZero_thenReturnNegativeInfinity() {
|
||||
assertEquals(Float.NEGATIVE_INFINITY, -12f / 0);
|
||||
assertEquals(Double.NEGATIVE_INFINITY, -12d / 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPositiveFloatingNumber_whenDividedByNegativeZero_thenReturnNegativeInfinity() {
|
||||
assertEquals(Float.NEGATIVE_INFINITY, 12f / -0f);
|
||||
assertEquals(Double.NEGATIVE_INFINITY, 12f / -0f);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenDividingFloatingNumberZeroByZero_thenReturnNaN() {
|
||||
assertEquals(Float.NaN, 0f / 0);
|
||||
assertEquals(Double.NaN, 0d / 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenABitRepresentationWithAllExponentBitsZeroesAndAllFractionBitsZeroes_whenTransformingItToFloat_thenReturnPositiveZero() {
|
||||
assertEquals(0f, Float.intBitsToFloat(0b00000000000000000000000000000000));
|
||||
assertEquals(-0f, Float.intBitsToFloat(0b10000000000000000000000000000000));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenABitRepresentationWithAllExponentBitsOnesAndAllFractionBitsZeroes_whenTransformingItToFloat_thenReturnInfinity() {
|
||||
assertEquals(Float.POSITIVE_INFINITY, Float.intBitsToFloat(0b01111111100000000000000000000000));
|
||||
assertEquals(Float.NEGATIVE_INFINITY, Float.intBitsToFloat(0b11111111100000000000000000000000));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenABitRepresentationWithAllExponentBitsOnesAndNotAllFractionBitsZeroes_whenTransformingItToFloat_thenReturnNan() {
|
||||
assertEquals(Float.NaN, Float.intBitsToFloat(0b11111111100000010000000000000000));
|
||||
assertEquals(Float.NaN, Float.intBitsToFloat(0b11111111100000011000000000100000));
|
||||
assertEquals(Float.NaN, Float.intBitsToFloat(0b11111111100000011100000000000000));
|
||||
assertEquals(Float.NaN, Float.intBitsToFloat(0b11111111100000011110000000000000));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.baeldung.formatNumber;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import static com.baeldung.formatNumber.FormatNumber.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class FormatNumberUnitTest {
|
||||
private static final double D = 4.2352989244d;
|
||||
private static final double F = 8.6994540927d;
|
||||
|
||||
@Test
|
||||
public void givenDecimalNumber_whenFormatNumberWithBigDecimal_thenGetExpectedResult() {
|
||||
assertThat(withBigDecimal(D, 2)).isEqualTo(4.24);
|
||||
assertThat(withBigDecimal(D, 3)).isEqualTo(4.235);
|
||||
assertThat(withBigDecimal(F, 2)).isEqualTo(8.7);
|
||||
assertThat(withBigDecimal(F, 3)).isEqualTo(8.699);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDecimalNumber_whenFormatNumberWithDecimalFormat_thenGetExpectedResult() {
|
||||
assertThat(withDecimalFormatLocal(D)).isEqualTo(4.235);
|
||||
assertThat(withDecimalFormatLocal(F)).isEqualTo(8.699);
|
||||
|
||||
assertThat(withDecimalFormatPattern(D, 2)).isEqualTo(4.24);
|
||||
assertThat(withDecimalFormatPattern(D, 3)).isEqualTo(4.235);
|
||||
assertThat(withDecimalFormatPattern(F, 2)).isEqualTo(8.7);
|
||||
assertThat(withDecimalFormatPattern(F, 3)).isEqualTo(8.699);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDecimalNumber_whenFormatNumberWithStringFormat_thenGetExpectedResult() {
|
||||
assertThat(withStringFormat(D, 2)).isEqualTo("4.24");
|
||||
assertThat(withStringFormat(D, 3)).isEqualTo("4.235");
|
||||
assertThat(withStringFormat(F, 2)).isEqualTo("8.70");
|
||||
assertThat(withStringFormat(F, 3)).isEqualTo("8.699");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDecimalNumber_whenFormatNumberWithMathRound_thenGetExpectedResult() {
|
||||
assertThat(withMathRound(D, 2)).isEqualTo(4.24);
|
||||
assertThat(withMathRound(D, 3)).isEqualTo(4.235);
|
||||
assertThat(withMathRound(F, 2)).isEqualTo(8.7);
|
||||
assertThat(withMathRound(F, 3)).isEqualTo(8.699);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntegerNumber_whenFormatNumberByPaddingOutZeros_thenGetExpectedResult() {
|
||||
int value = 1;
|
||||
assertThat(byPaddingZeros(value, 3)).isEqualTo("001");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntegerNumber_whenFormatNumberWithTwoDecimalPlaces_thenGetExpectedResult() {
|
||||
int value = 12;
|
||||
assertThat(withTwoDecimalPlaces(value)).isEqualTo(12.00);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntegerNumber_whenFormatNumberWithLargeIntegers_thenGetExpectedResult() {
|
||||
int value = 123456789;
|
||||
assertThat(withLargeIntegers(value)).isEqualTo("123,456,789");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDecimalNumber_whenFormatNumberForPercentages_thenGetExpectedResult() {
|
||||
double value = 25f / 100f;
|
||||
assertThat(forPercentages(value, new Locale("en", "US"))).isEqualTo("25%");
|
||||
assertThat(forPercentages(value, new Locale("pl", "PL"))).isEqualTo("25%");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCurrency_whenFormatNumberCurrencyWithChosenLocalisation_thenGetExpectedResult() {
|
||||
double value = 23_500;
|
||||
assertThat(currencyWithChosenLocalisation(value, new Locale("en", "US"))).isEqualTo("$23,500.00");
|
||||
assertThat(currencyWithChosenLocalisation(value, new Locale("zh", "CN"))).isEqualTo("¥23,500.00");
|
||||
assertThat(currencyWithChosenLocalisation(value, new Locale("pl", "PL"))).isEqualTo("23 500 zł");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.integerToBinary;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class IntegerToBinaryUnitTest {
|
||||
@Test
|
||||
public void givenAnInteger_whenConvertToBinary_thenGetBinaryString() {
|
||||
int n = 7;
|
||||
String binaryString = IntegerToBinary.convertIntegerToBinary(n);
|
||||
assertEquals("111", binaryString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnInteger_whenToBinaryStringCalled_thenGetBinaryString() {
|
||||
int n = 7;
|
||||
String binaryString = Integer.toBinaryString(n);
|
||||
assertEquals("111", binaryString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnInteger_whenToStringCalled_thenGetBinaryString() {
|
||||
int n = 7;
|
||||
String binaryString = Integer.toString(n, 2);
|
||||
assertEquals("111", binaryString);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user