Posts Tagged ‘Number’

Number in Flex3 and the decimal separator

Tuesday, February 3rd, 2009

Today, I decided to write my first blog post in English. The reason for this post is the implementation of Number in current Flex3 SDK. I haven’t used Number as input for a while, the last time about one or one and half years ago. What had changed during this time: You can’t directly parse a String into Number if you using a comma as decimal separator.I used the resource bundle properties to change the decimal and thousand separator.

SharedResources.properties:

# CurrencyFormatter, NumberFormatter, Slider
decimalSeparatorFrom=.
decimalSeparatorTo=,
thousandsSeparatorFrom=,
thousandsSeparatorTo=.

validators.properties:

# Currency/Number Validator
allowNegative=true
decimalSeparator=,
maxValue=NaN
minValue=NaN
thousandsSeparator=.

If you trying parse the String into Number using current 3.2.0 Flex SDK, the result is always NaN. In Flex 2 this transformation worked perfect.

var s:String = “0,5”

var n:Number = Number(s);

=> n = NaN

Also the parseFloat function can’t parse the String into Number. The function cuts off the decimal value of the Number if you using a comma. The only way to do this transformation is using a regular expression to replace the comma with a point and than transform the String into a Number. If you have multiple input fields with numbers, this is no satisfying solution.

When I read some JIRAs at the Adobe Bug and Issue Management System, I found a couple of bugs which are related to this problem. The NumberValidator and CurrencyValidator doesn’t work correct if you using a comma as decimal separator. They can’t validate if the number is in the range of min and max values of the validator. Before comparing the input against the min and max values they transform the String into Number by using following code.

var x:Number = Number(input);

The result of this transformation using a comma as decimal separator is always NaN.  Line 295 of the NumberValidator and line 343 of the CurrencyValidator Adobe uses this transformation. So, the validation of min and max couldn’t work using a comma.

Please, Adobe correct the implemtation of Number.