• Home
  • About

Snippet IT

IT News, Programming, Internet and Blogging

  • Programming and Scripting
  • Tips and Tricks
  • Software and Hardware
  • New and Happening
You are here: Home / Programming and Scripting / PHP: Format integer into number of decimal places

PHP: Format integer into number of decimal places

April 25, 2009 by Sze Hau Leave a Comment

Previous I have written an article about formating interger into number of decimal places in Java. The main reason to store money value in integer in to prevent loss in precision. For example the actual value for 12.33 in float could be 12.3333333333…; while 1233 in integer is always 1233.

Today I will be showing you the same code but in PHP language. I know you can do the same thing with PHP’s number_format(), but you will need to cast the integer into float first before past the money value into the number_format() function. Therefore there are possibles of lose in precision.

Here is the code:

 

function format_money($money, $comma = true) {
    $stack = array();
    $stackIndex = 0;

    $neg = $money < 0;
    if ($neg) $money = -$money;

    $prec = 2;
    while ($prec > 0) {
        $mod = $money % 10;
        $money = (int) ($money / 10);
        $stack[$stackIndex++] = ''.$mod;
        $prec--;
    }

    $stack[$stackIndex++] = '.';
    $firstZero = true;
    $commaCnt = 0;
    while ($firstZero || $money > 0) {
        $mod = $money % 10;
        $money = (int) ($money / 10);
        $stack[$stackIndex++] = $mod;
        $firstZero = false;
        $commaCnt ++;
        if ($comma && $commaCnt == 3) {
            $commaCnt = 0;
            if ($money > 0) $stack[$stackIndex++] = ',';
        }
    }

    if ($neg) $stack[$stackIndex++] = '-';
    $out = '';
    while (--$stackIndex >= 0) {
        $out.= $stack[$stackIndex];
    }
    return $out;
}


Examples of input and output:

1000 --> 10.00
100000 --> 1,000.00
-1000 --> -10.00
-100000 --> -1,000.00

Filed Under: Programming and Scripting Tagged With: decimal, format, how to, money, PHP

About Sze Hau

Geek. Love programming. Coffee addicted. Married with two children. Working towards financial freedom.

Leave a ReplyCancel reply

Advertisement

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

Software and Hardware

MD5 and SHA1 Checksum Using Windows

July 5, 2017 By Sze Hau Leave a Comment

Blog Network

  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • szehau's weblog Life, Internet, Software, Gadgets, Programming and Investments

Snippet IT

This is the place where I want to share anything about information technology.

Search

Recent

  • MD5 and SHA1 Checksum Using Windows
  • MD5 and SHA1 Checksum Using Linux
  • Java: Unlimited Strength Jurisdiction Policy
  • WordPress: How To Change Admin Username
  • Linux: How To Compress And Decompress Folders And Files

Tags

advertisement blog C# Chrome DecimalFormat EGPC Float format format Integer free icons Gmail Google Google Adsense Google Chrome Google Search Engine Google search result how to HTTP Java JavaScript Linux money password performance PHP programming search engine optimization secure security short URL site value spam SQL static constructor String thread tiny URL Tips and Tricks trackback twitter video Wordpress wordpress plugin wordpress theme Youtube

Copyright © 2025 · Magazine Pro Theme on Genesis Framework · WordPress · Log in