英文:
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 < 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("String replacement: " + (end - start));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论