I want to be able to convert a string number such as "1,427.76" to a number in coldfusion but the comma is making it fail. Is there a simple way to do it besides having to remove the comma?
<cfset string = "1,427.75"> <cfset number = string * 100>
The error occurs when trying to perform mathematical operations on it. If the comma is removed it works just fine but I'm getting the comma from a database calculation.
You convert a string to a number by calling the Parse or TryParse method found on numeric types ( int , long , double , and so on), or by using methods in the System. Convert class. It's slightly more efficient and straightforward to call a TryParse method (for example, int.
The atoi() function converts a character string to an integer value. The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type. The function stops reading the input string at the first character that it cannot recognize as part of a number.
Parse(String, NumberStyles) Converts the string representation of a number in a specified style to its Decimal equivalent.
I know you can use LSParseNumber:
<cfset string = "1,427.75"> <cfset number = LSParseNumber(string) * 100>
Val() works as well for simple conversions where you don't care about locale, e.g. Val('123.45')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With