FLEX XMLDecoder将`09.00`变为“09.00”,但将10.00`变为10

时间:2011-05-31 19:11:31

标签: flex actionscript-3

有人可以解释为什么FLEX 4.5 XMLDecoder会对我的XML数据执行此操作吗?

var decoder:XMLDecoder = new XMLDecoder;
var $object:Object = decoder.decode( <xmltag>08.00</xmltag> );
// object = "08.00"

var decoder:XMLDecoder = new XMLDecoder;
var $object:Object = decoder.decode( <xmltag>11.00</xmltag> );
// Object = "11" (HEY! Where did my '.00' part of the string go?)

var decoder:XMLDecoder = new XMLDecoder;
var $object:Object = decoder.decode( <xmltag>11.30</xmltag> );
// Object = "11.3" (HEY! Where did my '0' part of the string go?)

2 个答案:

答案 0 :(得分:1)

Flex解​​串器也给了我这个问题。它可能将它们解释为Number对象,因此当调用toString()时它们将返回短表示。

每当您需要打印.toFixed(2)

等值时,请尝试使用11.00
var $object:Object = decoder.decode( <xmltag>11.00</xmltag> );
trace($object); //11
trace($object.toFixed(2)); //11.00

答案 1 :(得分:1)

所以,回答为什么这个问题的原始问题:

source code for SimpleXMLDecoder(我猜它有与XMLDecoder类似的功能)中,函数simpleType()中有一个注释:

//return the value as a string, a boolean or a number.
//numbers that start with 0 are left as strings
//bForceObject removed since we'll take care of converting to a String or Number object later

以0开头的数字保留为字符串 - 我猜他们想到的是电话号码而不是小数。

另外,由于某些hacky implicit casting,您实际上有三种不同的类型 -

  • “0.800”:String
  • 11:int
  • 11.3:数字