Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.