用小数乘以小数位数
我需要一种将浮点数乘以小数位数的方法。
例如
Number = 10.04
Number of Decimal Places = 2
Result = 1004
Number = 123.421
Number of Decimal Places = 3
Result = 123421
,依此类推,我编写了一个方法来返回小数位数,但是我如何才能得到如上所述的预期结果呢?
I need a way to multiple the a float by the number of decimal places.
e.g.
Number = 10.04
Number of Decimal Places = 2
Result = 1004
Number = 123.421
Number of Decimal Places = 3
Result = 123421
so on and so forth, I have a method written to return the number of decimal places, but how can I expected result as mentioned above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
float
值没有 小数位。因此,如果你想做任何涉及小数位的事情,你必须停止使用float
。否则,您将不可避免地得到意想不到的(错误)结果。有关详细信息,请阅读浮点指南。
A
float
value does not have decimal places. So if you want to do anything that involves decimal places, you have to stop usingfloat
. Otherwise, you'll inevitably get unexpected (wrong) results.Read the Floating-Point Guide for details.
你只是移动小数点吗?
如果是这样的话...#编辑#
结果=数字*(10的小数点次方)
Are you just moving the decimal point?
If so... #EDITTED#
result = number * (10 to the power of decimal places)
还有另一种方法:
Float.toString(10.234f).replace(".", "")
返回 10234!
Got another way:
Float.toString(10.234f).replace(".", "")
returns 10234!!