Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

toFixed problem 2

Status
Not open for further replies.

k4ghg

Technical User
Dec 25, 2001
191
US
I have officially lost the three last strands of my hair and the scalp is starting to go, over this strange problem. I am trying to take a latitude in degrees, which is passed in a form and reduce the decimals to only 2 places for display.

Testing:
1) Check to make sure that the number from the form is passed to the function:
var lat2 = form1.lat2.value;
alert (lat2);

Works (i.e., 36.889 from form and that is displayed in the alert).


2) Try to reduce the decimals to two places:

var lat2 = form1.lat2.value;
alert (lat2.toFixed(2));

Fails, nothing is displayed. I am certain its an easy fix, I just don't know what to do.

Thanks... Ronnie
 
The issue stems from the fact that toFixed requires a number, but the value from a from input is always a string even if the string represents a number.

You need to make it an actual number.

Code:
  var lat2 = parseFloat(form1.lat2.value);
      alert (lat2.toFixed(2));

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks Phil, for some reason I knew it would be something simple. Ronnie
 
Mo Problem

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top