Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a string representation of a number to a number in coldfusion?

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.

like image 358
Scott Chantry Avatar asked Jan 25 '10 17:01

Scott Chantry


People also ask

How do I convert a string to a number?

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.

What function converts a string to a number?

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.

What is the name of the function that converts a string to a number with a decimal point?

Parse(String, NumberStyles) Converts the string representation of a number in a specified style to its Decimal equivalent.


2 Answers

I know you can use LSParseNumber:

<cfset string = "1,427.75">  <cfset number = LSParseNumber(string) * 100> 
like image 143
derivation Avatar answered Oct 06 '22 04:10

derivation


Val() works as well for simple conversions where you don't care about locale, e.g. Val('123.45')

like image 20
Matt Woodward Avatar answered Oct 06 '22 05:10

Matt Woodward