在javascript中的toString浮点数

时间:2016-11-07 05:50:19

标签: javascript typescript

在下面的代码段中,如何显示消息

Min value is 0.00000001. 

而不是

Min value is 1e-8



var minValue = 0.00000001;

var message =  "Min value is " +  minValue.toString();

var content = $('p#content').text(message);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="content"></p>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

使用Number#toFixed或使用字符串而不是数字。

var minValue = 0.00000001;

var message =  "Min value is " +  minValue.toFixed(8);

var content = $('p#content').text(message);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="content"></p>

答案 1 :(得分:0)

var minValue = "0.00000001";

var message =  "Min value is " +  minValue;

var content = $('p#content').text(message);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="content"></p>