0

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

1
  • do you have already tried? show us some code Commented Oct 21, 2013 at 9:13

5 Answers 5

2

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/

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

Try this

Math.round(num * 100) / 100

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.