用于小数位的JavaScript固定方法

时间:2018-02-09 06:06:14

标签: javascript

我不知道我的代码有什么问题。

  

使用toFixed()方法显示具有指定小数位数的数值。显示工资为2位小数的货币

var payRate=7.675;
var hours=25;
var total=payRate*hours;

console.log('You have earned $' + total);
console.log('You have earned $' + total.toFixed(2) + '.');

console.log()undefined

2 个答案:

答案 0 :(得分:1)

Undefined不是代码的返回值,它是console.log()的返回值

由于console.log()不返回任何内容,因此会打印undefined

高于undefined,您将拥有自己的价值,请再次运行您的代码,我会粘贴相同的代码

var payRate=7.675;
var hours=25;
var total=payRate*hours;

console.log('You have earned $' + total);
console.log('You have earned $' + total.toFixed(2) + '.');

请运行代码段并检查控制台

答案 1 :(得分:0)

您的代码运行正常,其打印效果如下 -

You have earned $191.875
You have earned $191.88.

PS:您可以查看浏览器版本。