Deprecated: Assigning the return value of new by reference is deprecated in /var/www/omflash.dk/public_html/wp-includes/cache.php on line 33

Deprecated: Assigning the return value of new by reference is deprecated in /var/www/omflash.dk/public_html/wp-content/plugins/sem-subscribe-me2/sem-subscribe-me.php on line 511

Strict Standards: call_user_func_array() expects parameter 1 to be a valid callback, non-static method UltimateTagWarriorActions::ultimate_query_vars() should not be called statically in /var/www/omflash.dk/public_html/wp-includes/functions.php on line 1252

Strict Standards: call_user_func_array() expects parameter 1 to be a valid callback, non-static method UltimateTagWarriorActions::ultimate_posts_where() should not be called statically in /var/www/omflash.dk/public_html/wp-includes/functions.php on line 1252

Strict Standards: call_user_func_array() expects parameter 1 to be a valid callback, non-static method UltimateTagWarriorActions::ultimate_posts_join() should not be called statically in /var/www/omflash.dk/public_html/wp-includes/functions.php on line 1252

Strict Standards: call_user_func_array() expects parameter 1 to be a valid callback, non-static method UltimateTagWarriorActions::ultimate_tag_templates() should not be called statically in /var/www/omflash.dk/public_html/wp-includes/functions.php on line 1329
AS3 - afrunding af tal, en lille gotcha - omFlash();
Strict Standards: call_user_func_array() expects parameter 1 to be a valid callback, non-static method UltimateTagWarriorActions::ultimate_add_ajax_javascript() should not be called statically in /var/www/omflash.dk/public_html/wp-includes/functions.php on line 1329

AS3 - afrunding af tal, en lille gotcha


Deprecated: preg_replace() [function.preg-replace]: The /e modifier is deprecated, use preg_replace_callback instead in /var/www/omflash.dk/public_html/wp-includes/functions-formatting.php on line 83

Strict Standards: call_user_func_array() expects parameter 1 to be a valid callback, non-static method UltimateTagWarriorActions::ultimate_the_content_filter() should not be called statically in /var/www/omflash.dk/public_html/wp-includes/functions.php on line 1252

Strict Standards: Non-static method UltimateTagWarriorActions::regExEscape() should not be called statically in /var/www/omflash.dk/public_html/wp-content/plugins/UltimateTagWarrior/ultimate-tag-warrior-actions.php on line 638

Strict Standards: Non-static method UltimateTagWarriorActions::regExEscape() should not be called statically in /var/www/omflash.dk/public_html/wp-content/plugins/UltimateTagWarrior/ultimate-tag-warrior-actions.php on line 638

Strict Standards: Non-static method UltimateTagWarriorActions::regExEscape() should not be called statically in /var/www/omflash.dk/public_html/wp-content/plugins/UltimateTagWarrior/ultimate-tag-warrior-actions.php on line 639

Strict Standards: Non-static method UltimateTagWarriorActions::regExEscape() should not be called statically in /var/www/omflash.dk/public_html/wp-content/plugins/UltimateTagWarrior/ultimate-tag-warrior-actions.php on line 639

Strict Standards: Non-static method UltimateTagWarriorActions::replaceTagWithLink() should not be called statically in /var/www/omflash.dk/public_html/wp-content/plugins/UltimateTagWarrior/ultimate-tag-warrior-actions.php on line 643

I min jagt efter en måde nemt at afrunde et tal til 2 decimaler, f.eks. fra 8,75469 og til 8,75, er jeg mange gange gået forgæves, for mystisk nok, så modtager Math.round() funktionen ingen argumenter, den afrunder altid til et helt tal.

Men, der findes rent faktisk en indbygget løsning, problemet for mig har bare været at jeg har haft stirret mig blind på Math.round() og Math-klassen generelt, for svaret findes i virkeligheden på Number og Int klassen.
De har nemlig begge to funktionen toFixed, som netop kan afrunde:

var num:Number = 8.45132;
var dec:String = num.toFixed(2);
trace(dec) //8.45

var danish:String = dec.split(".").join(",");
trace(danish) // 8,45

Som bonus har jeg også lige vist en simpel måde at ændre punktummet til et komma, hvis man har behov for at vise det til en dansk bruger.

Det her er en af de ting, hvor hvis man ved det, så er det helt åbenlyst, men gør man ikke, så er det nemt at stirre sig blind på Math klassen som jeg gjorde :-)



Del:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • digg

    Relaterede artikler:
    Strict Standards: Only variables should be assigned by reference in /var/www/omflash.dk/public_html/wp-includes/functions.php on line 615

    Strict Standards: Only variables should be assigned by reference in /var/www/omflash.dk/public_html/wp-includes/functions.php on line 615

    Strict Standards: Only variables should be assigned by reference in /var/www/omflash.dk/public_html/wp-includes/functions.php on line 615

    Strict Standards: Only variables should be assigned by reference in /var/www/omflash.dk/public_html/wp-includes/functions.php on line 615

    Strict Standards: Only variables should be assigned by reference in /var/www/omflash.dk/public_html/wp-includes/functions.php on line 615
  • Square colors - auto-genereret kunst
  • Fredrik Clement V2
  • AntiAliasType.ADVANCED + Filter = Trouble
  • Flickr Julehjertefletter v. 2
  • Flash Player 10.2 og StageVideo

4 kommentarer

  1. Mads Bjerre siger: (9. september 2009 kl. 15:00 )

    Deprecated: preg_replace() [function.preg-replace]: The /e modifier is deprecated, use preg_replace_callback instead in /var/www/omflash.dk/public_html/wp-includes/functions-formatting.php on line 83

    Godt tip - kendte det faktisk ikke. Dog værd at være opmærksom på, at toFixed() returnerer String og ikke Number.

    Hvis man vil holde sig udelukkende til tal og Math-klassen, kan man nemt afrunde til to decimaler bare ved at gange og dividere lidt (men det ved du sikkert allerede):

    var num : Number = 8.45132;
    var rounded : Number = Math.round(num * 100) / 100;
    trace(rounded) //8.45

  2. felisan siger: (9. september 2009 kl. 23:12 )

    Deprecated: preg_replace() [function.preg-replace]: The /e modifier is deprecated, use preg_replace_callback instead in /var/www/omflash.dk/public_html/wp-includes/functions-formatting.php on line 83

    fin info :)

  3. Mads Buch Stage siger: (10. september 2009 kl. 09:52 )

    Deprecated: preg_replace() [function.preg-replace]: The /e modifier is deprecated, use preg_replace_callback instead in /var/www/omflash.dk/public_html/wp-includes/functions-formatting.php on line 83

    @Mads Bjerre:
    Ja, det er rigtigt det med stringen, men 9 ud af 10 gange hvor jeg har brug for det, så er det i forbindelse med at vise et tal til brugeren - har sjældent være ude for at ville reducere præcisionen i mine beregninger ;-)

    Men som sagt, fin pointe - opdaterer lige eksemplet :-)

  4. mat siger: (14. september 2009 kl. 11:39 )

    Deprecated: preg_replace() [function.preg-replace]: The /e modifier is deprecated, use preg_replace_callback instead in /var/www/omflash.dk/public_html/wp-includes/functions-formatting.php on line 83

    Mht til split().join() finten, hvorfor så ikke vinde lidt af magten ved regular expressions og bruge String.replace()?

    var danish:String = dec.replace(/\./, “,”);

    :)

Skriv en kommentar