US_User
US_User

Reputation: 57

how to remove the decimal digit using the javascript

I need to show the value 29.89 like this. but not need to show like that 29.0, If after whole number is start with 0 means we need not show.

Example:

27.0 ==> 27 only

27.9 ==> 27.9 this is wright. 

how to remove the 0 from first one using javascript

Upvotes: 0

Views: 122

Answers (5)

Dattatray Walunj
Dattatray Walunj

Reputation: 175

You can add a function to Number object and use it anywhere in your page /project.

Add function to Number object

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

How to use

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

Upvotes: 0

Sunny
Sunny

Reputation: 3295

It seems, that the value is stored in string. Cast the value to float, and it should remove the point by itself.

Upvotes: 0

jgroenen
jgroenen

Reputation: 1326

This is the default behavior of JavaScript:

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

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

etc.

http://jsfiddle.net/4QYuR/

Upvotes: 2

Ed T.
Ed T.

Reputation: 1039

Use following parseFloat("YOUR-NUMBER") Here's an example how it works http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_parsefloat

Upvotes: 1

arulmr
arulmr

Reputation: 8836

Try this

Math.round(num * 100) / 100

Upvotes: 0

Related Questions