• 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

More from my site

  • Java: Format Integer Into Fixed Width StringJava: Format Integer Into Fixed Width String
  • Java: Format Long Integer Into Hexadecimal StringJava: Format Long Integer Into Hexadecimal String
  • Wordpress: How To Change Admin UsernameWordPress: How To Change Admin Username
  • Linux: How To Compress And Decompress Folders And FilesLinux: How To Compress And Decompress Folders And Files
  • Java: How To Select Top N Objects From A ListJava: How To Select Top N Objects From A List
  • Java: How To Create A Simple Web Server Using HttpServerJava: How To Create A Simple Web Server Using HttpServer

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 Reply Cancel reply

Advertisement

  • Facebook
  • Google+
  • Instagram
  • Twitter

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

Adsense advertisement advertising apache blog blogging tips C# EGPC error estimation format format Integer Gmail Google Google Adsense Google Chrome Google Search Engine Google search result how to HTTP internet marketing Java JavaScript Linux money password performance PHP programming search engine optimization secure security short URL SQL static constructor String tiny URL Tips and Tricks twitter video Windows Vista Wordpress wordpress plugin wordpress theme Youtube

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