NumberとparseFloat

by yasurageruheya
Number("1000.25,57") + 100 = Nan
parseFloat("1000.25,57") + 100 = 1100.25
速度差がほとんど無いなら、安心して変換出来るparseFloatしか使うべきではないのかな、と思いました。
♥0 | Line 107 | Modified 2015-02-02 20:21:37 | MIT License
play

ActionScript3 source code

/**
 * Copyright yasurageruheya ( http://wonderfl.net/user/yasurageruheya )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/uJc2
 */

package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;
    public class FlashTest extends Sprite {
        
        public const LOOP:int = 20000;
        
        public const txt:TextField = new TextField();
        
        public const const_vec:Vector.<TestObject> = new Vector.<TestObject>();
        
        public var var_vec:Vector.<TestObject>;
        
        public var tests:Vector.<Tester> = new Vector.<Tester>();
        
        public function FlashTest() {
            
            
            tests[tests.length] = new Tester("parseFloat('5.55') : ", function():void
            {
                var i:int = LOOP;
				const str:String = "5.55";
				while (i--)
				{
					parseFloat(str);
				}
            });
            
            tests[tests.length] = new Tester("Number('5.55') : ", function():void
            {
                var i:int = LOOP;
				const str:String = "5.55";
				while (i--)
				{
					Number(str);
				}
            });
            
            tests[tests.length] = new Tester("parseFloat('5555555555555.5') : ", function():void
            {
                var i:int = LOOP;
				const str:String = "5555555555555.5";
				while (i--)
				{
					parseFloat(str);
				}
            });
            
            tests[tests.length] = new Tester("Number('5555555555555.5') : ", function():void
            {
                var i:int = LOOP;
				const str:String = "5555555555555.5";
				while (i--)
				{
					Number(str);
				}
            });
            
            tests[tests.length] = new Tester("parseFloat('5.5555555555555') : ", function():void
            {
                var i:int = LOOP;
				const str:String = "5.5555555555555";
				while (i--)
				{
					parseFloat(str);
				}
            });
            
            tests[tests.length] = new Tester("Number('5.5555555555555') : ", function():void
            {
                var i:int = LOOP;
				const str:String = "5.5555555555555";
				while (i--)
				{
					Number(str);
				}
            });
            
            tests.reverse();
            txt.width = 500;
            txt.height = 500;
            addChild(txt);
            
            // write as3 code here..
            addEventListener(Event.ENTER_FRAME, test);
        }
        
        private function test(e:Event):void 
        {
            var str:String = "";
            var i:int = tests.length;
            while (i--)
            {
                str += tests[i].start();
            }
            txt.text = str;
        }
    }
}

class TestObject
{
    public function TestObject(){}
}

import flash.utils.getTimer;
class Tester
{
    public var time:Number = 0;
    
    public var name:String;
    
    public var test:Function;
    
    public var count:int = 1;
    
    public function start():String
    {
        const startTime:int = getTimer();
        test();
        time += getTimer() - startTime;
        return name + (time / (count++)) + " ms.\n";
    }
    
    public function Tester(name:String, test:Function)
    {
        this.name = name;
        this.test = test;
    }
}