to String without ScientificNotation

This commit is contained in:
YuCheng Hu 2020-02-25 09:30:18 -05:00
parent e2f1d5d333
commit 8be6c2e20d
1 changed files with 77 additions and 3 deletions

View File

@ -11,10 +11,21 @@ import java.math.MathContext;
import java.math.RoundingMode;
import java.util.Random;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
*/
public class BigDecimalDemoUnitTest {
private static final Logger logger = LoggerFactory.getLogger(BigDecimalDemoUnitTest.class);
@Test
public void whenBigDecimalCreated_thenValueMatches() {
BigDecimal bdFromString = new BigDecimal("0.1");
@ -117,4 +128,67 @@ public class BigDecimalDemoUnitTest {
.calculateTotalAmount(quantity, unitPrice, discountRate, taxRate);
assertEquals("11.68", amountToBePaid.toString());
}
/**
* To String without ScientificNotation
*/
@Test
public void bigDecimalWithoutScientificNotation() {
String input = RandomStringUtils.randomNumeric(12)
+ "12345678901234567"
+ "8901234567890123"
+ "4567890123456789"
+ "0123456789012345"
+ "6789012345678901"
+ "2345678901234567"
+ "8901234567890123"
+ "4567890123456789"
+ "0123456789012345"
+ "6789012345678901"
+ "2345678901234567"
+ "8901234567890123"
+ "4567890123456789"
+ "0123456789012345"
+ "6789012345678901"
+ "2345678901234567"
+ "8901234567890123"
+ "4554324324362432"
+ "7674637264783264"
+ "7832678463726478"
+ "3264736274673864"
+ "7364732463546354"
+ "6354632564532645"
+ "6325463546536453"
+ "6546325463546534"
+ "6325465345326456"
+ "4635463263453264";
// Converting to BigDecimal
BigDecimal bigNumber = new BigDecimal(input);
// Apply toString() method
String numberStr = bigNumber.toString();
// Print the result
logger.info("{}", numberStr);
}
@Test
public void bigDecimalScientificNotation() {
// Create a BigDecimal object
BigDecimal a;
// Create a String object
String s;
// Set precision to 5
MathContext mc = new MathContext(5);
a = new BigDecimal("4536785E4", mc);
// apply toString() method
s = a.toString();
// print the result
System.out.println(s);
}
}