Javascript js函数忽略十进制数

Javascript js函数忽略十进制数,javascript,php,Javascript,Php,所以我做了一个函数,通过乘以我放了多少米来计算价格问题是,当我放十进制数时,它忽略了它 这是我的剧本 <script> function getFillingPrice() { cake_prices = document.getElementById('price').value; filling_prices = document.getElementById('test2').value; var t=parseInt(filling_prices);

所以我做了一个函数,通过乘以我放了多少米来计算价格问题是,当我放十进制数时,它忽略了它

这是我的剧本

<script>
function getFillingPrice() {
    cake_prices = document.getElementById('price').value;
    filling_prices = document.getElementById('test2').value;
    var t=parseInt(filling_prices);
    var x=parseInt(cake_prices);
    return t*x;
}


function calculateTotal() {
    var total = getFillingPrice();
    var totalEl = document.getElementById('totalPrice');

    document.getElementById('test3').value =total + " دينار ";
    totalEl.style.display = 'block';
}
  </script>

函数getFillingPrice(){
cake_prices=document.getElementById('price').value;
填充价格=document.getElementById('test2')。值;
var t=parseInt(填充价格);
var x=parseInt(蛋糕价格);
返回t*x;
}
函数计算器总计(){
var total=getFillingPrice();
var totalEl=document.getElementById('totalPrice');
document.getElementById('test3')。value=total+;
totalEl.style.display='block';
}

从DOM获取值时,您正在将值转换为整数

改变这个

var t=parseInt(filling_prices);
var x=parseInt(cake_prices);
对此

var t=parseFloat(filling_prices);
var x=parseFloat(cake_prices);

当您从DOM获取值时,您正在将它们转换为整数

改变这个

var t=parseInt(filling_prices);
var x=parseInt(cake_prices);
对此

var t=parseFloat(filling_prices);
var x=parseFloat(cake_prices);

除了解析问题之外,您还可以使用

  • 安和

  • 非可解析值的默认值,如字母或带字符的空字符串(值)


一起

function getFillingPrice() {
    var cake_price = +document.getElementById('price').value || 0,
        filling_price = +document.getElementById('test2').value || 0;

    return cake_price * filling_price;
}

除了解析问题之外,您还可以使用

  • 安和

  • 非可解析值的默认值,如字母或带字符的空字符串(值)


一起

function getFillingPrice() {
    var cake_price = +document.getElementById('price').value || 0,
        filling_price = +document.getElementById('test2').value || 0;

    return cake_price * filling_price;
}

parseInt
照它说的做,解析一个INT。。。你想要的是parseFloat,而不是
parseInt
按照它说的做,解析一个INT。。。如果用户输入的是空格或字母,这会解决非数字问题吗?在这种情况下,它返回零。您可以试试。@Mynameisjeff
NaN
是一个错误的值。如果用户输入的是空格或字母,这会解决非数字问题吗?在这种情况下,它返回零。您可以试试。@Mynameisjeff
NaN
是一个错误的值。