February 3rd, 2009

Number in Flex3 and the decimal separator

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.

One Response to “Number in Flex3 and the decimal separator”

  1. Thanks for the

    This should really be fixed. I’m using a HTTPService with the resultFormat=”object”. If you don’t specify a XMLDecode-function, a SimpleXMLDecoder is used.

    The SimpleXMLDecoder (line 63) uses “new Number()” to create number-objects. When a value “76.560” is in the XML (dutch notation, the thousandsseparator is the dot) -> Number is parsed as “76.56”. So even when I did a regex-replace on “.” in the results, I got the wrong number because the harm was already done in the object-tree.

    I ended up extending SimpleXMLDecoder and using custom number-generate method.