Simply Program

Simply programming - It’s a life style
Filed under Programming, Javascript

In some situations we are required to take a value that was intended to be a string and convert it to a numerical value for calculations.

In this situation we would implement a form of variable control called "Casting".

CODE:
  1. //String
  2. var string = "5.00";
  3.  
  4. //What would happen if we wanted to add another 5.00 to string?
  5. //Well, it would append the 5.00 to the original 5.00 - Example: 5.005.00
  6. //We need to Cast "string" so that we can do numerical calculations.
  7. var numeric = parseFloat(string)+5.00;
  8. //You can use
  9. //parseFloat - number with a decimal point.
  10. //parseInt - whole number.

Simple and effective.

Posted by sp on Thursday, July 10th, 2008


You can follow any responses to this entry through the magic of "RSS 2.0" and leave a trackback from your own site.