Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript删除十进制数字_Javascript_Numbers - Fatal编程技术网

如何使用javascript删除十进制数字

如何使用javascript删除十进制数字,javascript,numbers,Javascript,Numbers,我需要这样显示值29.89。但不需要这样显示29.0,如果整数以0开头,则表示不需要显示 Example: 27.0 ==> 27 only 27.9 ==> 27.9 this is wright. 如何使用javascript从第一个文件中删除0,请尝试以下操作 Math.round(num * 100) / 100 试试这个 Math.round(num * 100) / 100 试试这个 Math.round(num * 100) / 100 试试这个 Math

我需要这样显示值29.89。但不需要这样显示29.0,如果整数以0开头,则表示不需要显示

Example:

27.0 ==> 27 only

27.9 ==> 27.9 this is wright. 
如何使用javascript从第一个文件中删除0,请尝试以下操作

Math.round(num * 100) / 100
试试这个

Math.round(num * 100) / 100
试试这个

Math.round(num * 100) / 100
试试这个

Math.round(num * 100) / 100

使用以下
parseFloat(“您的号码”)

下面是它的工作原理示例

使用以下
parseFloat(“您的号码”)

下面是它的工作原理示例

使用以下
parseFloat(“您的号码”)

下面是它的工作原理示例

使用以下
parseFloat(“您的号码”)

下面是它的工作原理示例

这是JavaScript的默认行为:

alert(29.100) => "29.1"
alert(28.000) => "28"

document.body.innerHTML = 29.100 => 29.1
document.body.innerHTML = 28.000 => 28

etc.

这是JavaScript的默认行为:

alert(29.100) => "29.1"
alert(28.000) => "28"

document.body.innerHTML = 29.100 => 29.1
document.body.innerHTML = 28.000 => 28

etc.

这是JavaScript的默认行为:

alert(29.100) => "29.1"
alert(28.000) => "28"

document.body.innerHTML = 29.100 => 29.1
document.body.innerHTML = 28.000 => 28

etc.

这是JavaScript的默认行为:

alert(29.100) => "29.1"
alert(28.000) => "28"

document.body.innerHTML = 29.100 => 29.1
document.body.innerHTML = 28.000 => 28

etc.

看起来,该值存储在字符串中。将该值强制转换为float,它应该自己删除该点

看起来,该值存储在字符串中。将该值强制转换为float,它应该自己删除该点

看起来,该值存储在字符串中。将该值强制转换为float,它应该自己删除该点

看起来,该值存储在字符串中。将该值强制转换为float,它应该自己删除该点

您可以向Number对象添加一个函数,并在页面/项目中的任何位置使用它

向数字对象添加函数

Number.prototype.myPrecision = function(){
    if(Math.round(this)==this){
      return parseInt(this);
    }
    else{
      return this.toFixed(2);
    }
}
如何使用

var n1 = 10.11;
var new_n1 = n1.myPrecision(); // This will output 10.11

//Other case
var n2 = 10.00;
var new_n2 = n2.myPrecision(); // This will output 10

您可以向Number对象添加一个函数,并在页面/项目中的任何位置使用它

向数字对象添加函数

Number.prototype.myPrecision = function(){
    if(Math.round(this)==this){
      return parseInt(this);
    }
    else{
      return this.toFixed(2);
    }
}
如何使用

var n1 = 10.11;
var new_n1 = n1.myPrecision(); // This will output 10.11

//Other case
var n2 = 10.00;
var new_n2 = n2.myPrecision(); // This will output 10

您可以向Number对象添加一个函数,并在页面/项目中的任何位置使用它

向数字对象添加函数

Number.prototype.myPrecision = function(){
    if(Math.round(this)==this){
      return parseInt(this);
    }
    else{
      return this.toFixed(2);
    }
}
如何使用

var n1 = 10.11;
var new_n1 = n1.myPrecision(); // This will output 10.11

//Other case
var n2 = 10.00;
var new_n2 = n2.myPrecision(); // This will output 10

您可以向Number对象添加一个函数,并在页面/项目中的任何位置使用它

向数字对象添加函数

Number.prototype.myPrecision = function(){
    if(Math.round(this)==this){
      return parseInt(this);
    }
    else{
      return this.toFixed(2);
    }
}
如何使用

var n1 = 10.11;
var new_n1 = n1.myPrecision(); // This will output 10.11

//Other case
var n2 = 10.00;
var new_n2 = n2.myPrecision(); // This will output 10

你已经试过了吗?给我们看看你有没有试过?给我们看看你有没有试过?给我们看看你有没有试过?给我们看一些代码