Java的BigDecimal长整型与小数转换

huangapple go评论78阅读模式
英文:

Java BigDecimal long with decimal conversion

问题

我有一个 BigDecimal 值为 123.45。我想将它转换为一个 long 值为 12345,并且想知道最佳的实现方式。

我宁愿避免像下面这样做:
val.multiply(new BigDecimal(100)).toBigInteger()

英文:

I have a BigDecimal value of 123.45. I'd like to convert it to a long value of 12345 and I wonder what's the best way of doing this.

I'd rather avoid doing something like
val.multiply(new BigDecimal(100)).toBigInteger()

答案1

得分: 1

使用BigDecimal的toString方法,替换字符串中的".",然后使用Long.valueOf。

英文:

Use to string of BigDecimal and string replace the "." and do a Long.valueOf

答案2

得分: 1

以下是翻译好的内容:

仅供参考,我将这个内容发布出来。进行了性能测试,如预期的那样,字符串替换的性能表现最差。

结果:
movePointRight:1230
multiply:833
scaleByPowerOfTen:591
字符串替换:3289

BigDecimal val = new BigDecimal(0);
long start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
    BigDecimal d = new BigDecimal(123.45);
    val = d.movePointRight(2);
}
long end = System.currentTimeMillis();
System.out.println("movePointRight: " + (end - start));

BigDecimal ten = new BigDecimal(10);
start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
    BigDecimal d = new BigDecimal(123.45);
    val = d.multiply(new BigDecimal(100));
}

end = System.currentTimeMillis();
System.out.println("multiply: " + (end - start));

start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
    BigDecimal d = new BigDecimal(123.45);
    val = d.scaleByPowerOfTen(2);
}

start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
    BigDecimal d = new BigDecimal(123.45);
    val = d.scaleByPowerOfTen(2);
}
end = System.currentTimeMillis();
System.out.println("scaleByPowerOfTen: " + (end - start));

start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
    BigDecimal d = new BigDecimal(123.45);
    val = new BigDecimal(d.toString().replace(".", ""));
}
end = System.currentTimeMillis();
System.out.println("字符串替换: " + (end - start));
英文:

Just posting this in case you want to know. Made a test for performances, as expected String replacement performed the worst.

results:
movePointRight: 1230
multiply: 833
scaleByPowerOfTen: 591
String replacement: 3289

    BigDecimal val = new BigDecimal(0);
long start = System.currentTimeMillis();
for(int i = 0; i &lt; 1000000; i++) {
BigDecimal d = new BigDecimal(123.45);
val = d.movePointRight(2);
}
long end = System.currentTimeMillis();
System.out.println(&quot;movePointRight: &quot; + (end - start));
BigDecimal ten = new BigDecimal(10);
start = System.currentTimeMillis();
for(int i = 0; i &lt; 1000000; i++) {
BigDecimal d = new BigDecimal(123.45);
val = d.multiply(new BigDecimal(100));
}
end = System.currentTimeMillis();
System.out.println(&quot;multiply: &quot; + (end - start));
start = System.currentTimeMillis();
for(int i = 0; i &lt; 1000000; i++) {
BigDecimal d = new BigDecimal(123.45);
val = d.scaleByPowerOfTen(2);
}
start = System.currentTimeMillis();
for(int i = 0; i &lt; 1000000; i++) {
BigDecimal d = new BigDecimal(123.45);
val = d.scaleByPowerOfTen(2);
}
end = System.currentTimeMillis();
System.out.println(&quot;scaleByPowerOfTen: &quot; + (end - start));
start = System.currentTimeMillis();
for(int i = 0; i &lt; 1000000; i++) {
BigDecimal d = new BigDecimal(123.45);
val =  new BigDecimal(d.toString().replace(&quot;.&quot;, &quot;&quot;));
}
end = System.currentTimeMillis();
System.out.println(&quot;String replacement: &quot; + (end - start));

huangapple
  • 本文由 发表于 2020年9月21日 19:56:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63991791.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定