本文介绍了摆脱科学计数法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要进行一些计算,但是我遇到一个值非常低的问题,例如,我需要得到0.005的2.7%,最后得到1.3500000000000003e-4,这不是我想要的。在寻找时,我只需要知道如何获得这些值的准确百分比,我现在正在做的是< value> * 2.7 / 100 对于整数或大于0.05的浮点数非常有用。

I need to do some calculations but I'm getting a problem with values which are very low, for example, I need to get the 2.7% of 0.005 and I end up with 1.3500000000000003e-4 which is not what I'm looking for, I just need to know how can I get an accurate percetage of those values, what I'm doing right now is <value> * 2.7 / 100 which works great for integers or floats greater than 0.05.

例如,我需要的是2.7% 0.005需要显示为0.000135。

For example, the one that I need which is 2.7% of 0.005 needs to be shown as 0.000135.

推荐答案

首先,请理解该语言没有损坏,只是。

First, understand that the language isn't broken, it's just that computers are just really bad at doing floating point math.

因此,在 1.3500000000000003e-4 是正确的,但是这里的问题是Elixir以指数表示法打印(非常小和非常大)浮点数。有几种方法可以将其打印为 0.000135

So, in a sense 1.3500000000000003e-4 is accurate, but your issue here is that Elixir prints the (very small and very large) floats in exponent notation. There are a few ways you can print it as 0.000135:


  • 使用Erlang的 float_to_binary

:erlang.float_to_binary(0.005 * 2.7 / 100, [:compact, {:decimals, 10}])
#=> "0.000135"


  • 使用:io.format

    :io.format("~f~n",[0.005 * 2.7 / 100])
    #=> "0.000135"
    


  • 或使用是围绕着Erlang的:io 模块

    请注意结果是字符串,并且不是上述示例中的数字-因为您只是将其格式化/打印为十进制。

    这篇关于摆脱科学计数法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 07-23 22:57