我的表有小数值。我的问题是如何转换2位小数点后的所有十进制值。
示例:
1)我的表列值为11.9和6.0。如何转换为此格式11.90和06.00
2)1045.7至1045.70
我正在使用django来显示使用这种格式的.my html表格代码而不是如何转换tofixed
<table class="table table-striped table-condensed table-bordered " id="child_table">
<thead class="btn-primary" >
<tr>
<th >Year</th>
<th >Estimated Mid-Year-Population</th>
<th colspan="3" >Infanticide</th>
<th colspan="3" >Murder</th>
<th colspan="3" >Rape</th>
<th colspan="3" >Kidnapping & Abduction</th>
<th colspan="3" >Abetment Of Suicide</th>
<th colspan="3" >Child Marriage Restraint Act</th>
<th colspan="3" >Other Crimes</th>
</tr>
</thead>
<td></td>
<th></th>
<th>(I)ncidance</th>
<th>(R)ate</th>
<th>%</th>
<th>I</th>
<th>R</th>
<th>%</th>
<th>I</th>
<th>R</th>
<th>%</th>
<th>I</th>
<th>R</th>
<th>%</th>
<th>I</th>
<th>R</th>
<th>%</th>
<th>I</th>
<th>R</th>
<th>%</th>
<th>I</th>
<th>R</th>
<th>%</th>
</tr>
<tbody id="crimec">
{% for query_all in crime_filter %}
<tr id="crime_child">
<td>{{ query_all.year}}</td>
<td>{{ query_all.mid_year_population }}</td>
<td>{{ query_all.infanticide_incidence }}</td>
{% if query_all.infanticide_rate > 0 %}
<td>{{ query_all.infanticide_rate }}</td>
{% else %}
<td> </td>
{% endif %}
<td>{{ query_all.infanticide_percentage }}</td>
<td>{{ query_all.murder_incidence }}</td>
<td>{{ query_all.murder_rate }}</td>
<td>{{ query_all.murder_percentage }}</td>
<td>{{ query_all.rape_incidence }}</td>
<td>{{ query_all.rape_rate }}</td>
<td>{{ query_all.rape_percentage }}</td>
<td>{{ query_all.kidnapping_abduction_i }}</td>
<td>{{ query_all.kidnapping_abduction_r }}</td>
<td>{{ query_all.kidnapping_abduction_p }}</td>
<td>{{ query_all.abetment_suicide_i }}</td>
{% if query_all.abetment_suicide_r > 0 %}
<td>{{ query_all.abetment_suicide_r }}</td>
{% else %}
<td> </td>
{% endif %}
<td>{{ query_all.abetment_suicide_p }}</td>
<td>{{ query_all.child_marriage_act_i }}</td>
{% if query_all.child_marriage_act_r > 0%}
<td>{{ query_all.child_marriage_act_r }}</td>
{% else %}
<td> </td>
{% endif %}
<td>{{ query_all.child_marriage_act_p }}</td>
<td>{{ query_all.total_i }}</td>
<td>{{ query_all.total_r }}</td>
<td>{{ query_all.total_p }}</td>
</tr>
{% endfor %}
</tbody>
</table>
答案 0 :(得分:1)
var value = 11.9;
if(!isInt(value)){
var changeValue = value.toFixed(2);
}
console.log(changeValue); // returns 11.90
// check int or float;
function isInt(n) {
return n % 1 === 0;
}
答案 1 :(得分:1)
您可以使用Number.toFixed()方法。
var num = 5.5;
console.log(num.toFixed(2)) // this will return 5.50
答案 2 :(得分:1)
答案 3 :(得分:1)
CODE:
var val=6;
var precision=2;
val = val.toFixed(precision);
if( val < 10.00 )
val = "0"+val
alert(val)