developer tip

BigDecimal을 문자열로

copycodes 2020. 11. 30. 17:56
반응형

BigDecimal을 문자열로


BigDecimal 개체가 있고이를 문자열로 변환하고 싶습니다. 문제는 내 값이 분수를 얻고 큰 숫자 (길이)를 얻고 문자열의 원래 숫자 만 필요하다는 것입니다.

BigDecimal bd = new BigDecimal(10.0001)
System.out.println(bd.toString());
System.out.println(bd.toPlainString());

출력은 다음과 같습니다.

10.000099999999999766941982670687139034271240234375
10.000099999999999766941982670687139034271240234375

그리고 나는 정확히 10.0001문자열 의 숫자가되도록 출력이 필요 합니다.


정확하게 얻으려면 10.0001String 생성자 또는 valueOf(double의 표준 표현을 기반으로 BigDecimal을 생성합니다) :

BigDecimal bd = new BigDecimal("10.0001");
System.out.println(bd.toString()); // prints 10.0001
//or alternatively
BigDecimal bd = BigDecimal.valueOf(10.0001);
System.out.println(bd.toString()); // prints 10.0001

문제 new BigDecimal(10.0001)는 인수가 a double이고 double이 10.0001정확히 표현할 수 없다는 것입니다. 따라서 10.0001가능한 가장 가까운 두 배로 "변환"됩니다 . 10.000099999999999766941982670687139034271240234375이것이 바로 여러분의 BigDecimal쇼입니다.

따라서 double 생성자를 사용하는 것은 거의 의미가 없습니다.

여기에서 더 많은 것을 읽을 수 있습니다. 소수점 이하 자리를 두 배로 옮기십시오.


귀하는 BigDecimal수를 포함하지 않는 10.0001당신이 그것을 초기화하기 때문에, double그리고는 double하지 않았다 확실히 당신이 한 생각의 수를 포함한다. (이것은의 요점입니다 BigDecimal.)

대신 문자열 기반 생성자 를 사용 하는 경우 :

BigDecimal bd = new BigDecimal("10.0001");

... 그런 다음 실제로 예상 한 숫자가 포함됩니다.


아래 메소드를 사용하여 java.math.BigDecimal을 String으로 변환 할 수 있습니다.

   BigDecimal bigDecimal = new BigDecimal("10.0001");
   String bigDecimalString = String.valueOf(bigDecimal.doubleValue());
   System.out.println("bigDecimal value in String: "+bigDecimalString);

출력 :
문자열의 bigDecimal 값 : 10.0001


다른 로케일을 더 잘 지원하려면 다음 방법을 사용하십시오.

DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
df.setMinimumFractionDigits(0);
df.setGroupingUsed(false);

df.format(bigDecimal);

또한 사용자 정의 할 수 있습니다.

DecimalFormat df = new DecimalFormat("###,###,###");
df.format(bigDecimal);

// Convert BigDecimal number To String by using below method //

public static String RemoveTrailingZeros(BigDecimal tempDecimal)
{
    tempDecimal = tempDecimal.stripTrailingZeros();
    String tempString = tempDecimal.toPlainString();
    return tempString;
}

// Recall RemoveTrailingZeros
BigDecimal output = new BigDecimal(0);
String str = RemoveTrailingZeros(output);

The BigDecimal can not be a double. you can use Int number. if you want to display exactly own number, you can use the String constructor of BigDecimal .

like this:

BigDecimal bd1 = new BigDecimal("10.0001");

now, you can display bd1 as 10.0001

So simple. GOOD LUCK.


To archive the necessary result with double constructor you need to round the BigDecimal before convert it to String e.g.

new java.math.BigDecimal(10.0001).round(new java.math.MathContext(6, java.math.RoundingMode.HALF_UP)).toString()

will print the "10.0001"


If you just need to set precision quantity and round the value, the right way to do this is use it's own object for this.

BigDecimal value = new BigDecimal("10.0001");
value = value.setScale(4, RoundingMode.HALF_UP);
System.out.println(value); //the return should be "10.0001"

One of the pillars of Oriented Object Programming (OOP) is "encapsulation", this pillar also says that an object should deal with it's own operations, like in this way:

참고URL : https://stackoverflow.com/questions/13900204/bigdecimal-to-string

반응형