Jump to content

formatting numbers in php


eccehomo

Recommended Posts

hi to all,

 

i know this is an old problem.

well, i just wanted to format a number to whatever format.

 

for example if i have 100000 i could format it to 100,000.00 by simply using a funtion like

 

echo format_number(100000,'###,###,###.#0');

 

output: 100,000.00

 

there is a similar function here: http://au3.php.net/manual/en/function.number-format.php#51219

but i guess that was only for formatting phone numbers..

 

what i have in mind is something very flexible...so that if declare:

echo format_number(1,'0000000000');

 

the output will be: 0000000001

 

i hope someone could help find the right function(s)...

 

the function i have in mind is very similar to C# or JAVA's Number.Format('###,###,###,#0');

 

i guess there is a php developer who did this already...care to share? :)

 

thanks in advance :D

 

 

Link to comment
https://forums.phpfreaks.com/topic/53703-formatting-numbers-in-php/
Share on other sites

<?php

// $number ~ the original number
// $length ~ the length to pad the number to

function format_number($number, $length) {
$currentlength = strlen($number);
$add = ($length - $currentlength) + 1;
$arg = "%0{$add}d";
return sprintf($arg, $number);
}

echo format_number(1, 2); // 01

?>

Is this what you want?

 

<?php
function prepend_zero($number, $n=2)
{
return (strlen($number) < $n+1) ? str_repeat('0',$n-strlen($number)).(string)$number : $number;
}

echo prepend_zero(1, 10); // Output: 0000000001
?>

 

im sorry, but that's not it..

heres what

function format_number($number, $format)
{
   /*
       code here that im looking for 
  */
   return $result;
}

echo format_number(1000,'###,###,###.#0') //output will be 1,000.00
echo format_number(1000.6,'###,###,###.#0') //output will be 1,000.60
echo format_number(1,'0000000000') //output will be 0000000001
echo format_number(1000000000,'0000000000') //output will be 1000000000
echo format_number(1000000000,'###,###,###.#0') //output will be 1,000,000,000.00
echo format_number(1000000000.5,'###,###,###.00') //output will be 1,000,000,000.00
echo format_number(1000000000.578,'###,###,###.#0') //output will be 1,000,000,000.58

 

if you have tried C# or java, or even borland delphi there is a very similar built-in function like this...

if we could just have this in php, it would be very helpful for our future projects...

thanks.:D

I know a little Java.

 

It is like this you mean then, right?:

import java.text.*;

public class FormatNumber
{
public static void main(String[] args)
{
	int number = 1000000000;
	DecimalFormat formatter = new DecimalFormat("#,###,###,###");

	String formatted = formatter.format(number);

	System.out.format("Unformatted: %d%n", number);
	System.out.println("Formatted: "+formatted);
}
}

 

Wouldn't a combination of PHP's number_format() and the function I posted before do the same?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.