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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting string to number and back again -> HELP!

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
1stly, Happy New Year all :)

I have a form which have currency fields, so I need to accept commas and decimal points.

I'm trying to do a calculation with the fields and I am having problems.

The calculation is as follows.....

Loan Amount = 50,000.00
Property Value = 100,000.00

So I want to calculate the LTV (Loan to Value)

However although I'm using parseFloat, it seems to stop at the comma.

So I type 50,000.00 into loan amount , as I type into property value, all i enter is 100 but it is showing 50% in the LTV field.

Thus it must only be seing 50 in the loan amount not 50 thousand!

is there a built in function to take a standard currency field and parse it as a number then convert back to currency or do I have to start messing about with regex's ?

thanks,

1DMF.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
it's ok I came up with this from another site
Code:
// Formatting routines
function stripNonDecimal(value) {
  var result = "";
  var c;              // The current character.

  value += ""; // Force conversion to string

  for (var i=0; i<value.length; i++) {
    c = value.charAt(i);
    if (((c >= "0") && (c <= "9")) || (c == ".")) {
      result += c;
    }
  }
  return result;
}

function parseDecimal(value) {
  return parseFloat(stripNonDecimal(value));
}

:)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
1DMF, that's a clunky function to do something relatively simple using (guess what?) regexp:
Code:
function stripNonDecimal(value) {
[s]  var result = "";
  var c;              // The current character.

  value += ""; // Force conversion to string

  for (var i=0; i<value.length; i++) {
    c = value.charAt(i);
    if (((c >= "0") && (c <= "9")) || (c == ".")) {
      result += c;
    }
  }
  return result;[/s]
   return value.replace(/[^0-9.]/g, "");
}

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
yeah I know, but it worked and I was being lazy on my first day back, happy new year to you.

you can always trust sexy kaht to come along with a sexy regex ;-)



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top