Link to home
Start Free TrialLog in
Avatar of Whing Dela Cruz
Whing Dela CruzFlag for Anguilla

asked on

toFixed not working

Hi experts, I'm having problem getting toFixed (Javascript). Please correct my code below. Thanks!

		var x = (this.responseText); 
		alert(x.toFixed(0));      
		alert(x.toFixed(2));        

Open in new window

Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Your code works.

Try
var x = (13); 
alert(x.toFixed(0));      
alert(x.toFixed(2));  

Open in new window


You are not providing the actual error you are getting. My guess it has to do with
(this.responseText)

Open in new window


Try outputting your response.Text to see if it looks like what you expect.
Avatar of Whing Dela Cruz

ASKER

Hi Scott, I  tried "var x = (13);" and its working. I'm having error when  using (this.responseText). An error says; "x.toFixed(2 is not s function"
Did you find out what this.responseText is outputting?
The (this.responseText) is 499,212 I got that result when using alert(this.responseText)  alone.
Hi Scott, I attached here the codes which have a perfect scenario of my problem above. Hope you can extend your help.

<!DOCTYPE html>
<html>
<body>
<input type="text" id="a1"/><br/><br/>

<input type="text" id="a2"/><br/><br/>

<button onclick="myFunction()">Try it</button>


<script>
function myFunction() 
  {
    var str = document.getElementById('a1').value;
    document.getElementById('a2').value = (str.toFixed(2));
  }
</script>

</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you so much Scott, Its working. More power to you, and  God bless!