如何显示小数值但删除小数

时间:2016-03-24 02:52:14

标签: format decimal

假设我的价值为1.25美元。我希望只显示.25,但删除小数,这样我就可以显示你有1美元和25美分。

1 个答案:

答案 0 :(得分:0)

假设javascript和html很好,你可以这样做:

<div id="output"></div> 

<script>
var cost = 1.25;
var beforeDec = Math.abs((cost).toFixed(0));
var afterDec = 100*(cost - beforeDec);

if (beforeDec < 1) {
var dollar = " dollars";
} else {
var dollar = " dollar";
}

if (afterDec > 1) {    
var cent = " cents"; 
} else {
var cent = " cent";
}


document.getElementById("output").innerHTML = "You have " + beforeDec + dollar + " and " + afterDec + cent + ".";

</script>

您可以尝试使用jsfiddle here