Number Serialization…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package
{
	import flash.display.Sprite;
	import flash.utils.ByteArray;
 
	public class Main extends Sprite
	{
		public function Main()
		{
			var bytes:ByteArray = new ByteArray();
			bytes.writeDouble(0.1);
			bytes.writeFloat(0.1);
			bytes.writeObject(new BigDecimal(0.1));
			bytes.position = 0;
 
			trace(bytes.readDouble());
			// 0.1 
 
			trace(bytes.readFloat());
			// 0.10000000149011612
			// Ô_ô
 
			trace(BigDecimal(bytes.readObject()).value);
			// 0.1
			// n_n
		}
	}
}
 
import flash.net.registerClassAlias;
import flash.utils.IDataInput;
import flash.utils.IDataOutput;
import flash.utils.IExternalizable;
 
internal class BigDecimal implements IExternalizable
{
	{
		registerClassAlias("BigDecimal", BigDecimal);
	}
 
	public var value:Number;
 
	public function BigDecimal(value:Number = NaN)
	{
		this.value = value;
	}
 
	public function readExternal(input:IDataInput):void
	{
		value = parseFloat(input.readUTF());
	}
 
	public function writeExternal(output:IDataOutput):void
	{
		output.writeUTF(value.toString());
	}
}

About this entry