eccehomo Posted May 31, 2007 Share Posted May 31, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/53703-formatting-numbers-in-php/ Share on other sites More sharing options...
Daniel0 Posted May 31, 2007 Share Posted May 31, 2007 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/53703-formatting-numbers-in-php/#findComment-265422 Share on other sites More sharing options...
chigley Posted May 31, 2007 Share Posted May 31, 2007 <?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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/53703-formatting-numbers-in-php/#findComment-265423 Share on other sites More sharing options...
eccehomo Posted June 1, 2007 Author Share Posted June 1, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/53703-formatting-numbers-in-php/#findComment-266089 Share on other sites More sharing options...
Daniel0 Posted June 1, 2007 Share Posted June 1, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/53703-formatting-numbers-in-php/#findComment-266135 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.