you can get the German format 1.234,56. (Comma as decimal separator and point as thousand separator)

But I have a problem with that: I want to add commas as thousand separators and change the decimal-separator (this could also be done with str_replace), but I do not want to change the amount of fractional digits!

But since the 2nd argument of number_format is necessary to enter the 3rd and 4th argument, this cannot be done with number_format. You have to change the fractional digits with this function.

But I want that 1234.56 changes into 1.234,56 and 1234.567890123456 changes into 1.234,567890123456

So, I created following function, that doesn't change the amount of fractional digits:

I'm not sure if this is the right place anyway, but "ben at last dot fm"'s ordinal function can be simplified further by removing the redundant "floor" (the result of floor is still a float, it's the "%" that's converting to int) and outer switch.

Note that this version also returns the number with the suffix on the end, not just the suffix.

I'd like to comment to the old notes of "stm555" and "woodynadobhar".They wrote about "number_format_unlimited_precision()".I guess many of us need that kind of function, which is the almost same function as number_format but don't round a number.

Does Anyone know any new solution in a recent PHP version?

...If no, how about the following function? (I fixed somethings like bugs of the function in the old comment.)<?phpfunction number_format_unchanged_precision($number, $dec_point='.', $thousands_sep=','){ if($dec_point==$thousands_sep){trigger_error('2 parameters for ' . __METHOD__ . '() have the same value, that is "' . $dec_point . '" for $dec_point and $thousands_sep', E_USER_WARNING);// It corresponds "PHP Warning: Wrong parameter count for number_format()", which occurs when you use $dec_point without $thousands_sep to number_format().} if(preg_match('{\.\d+}', $number, $matches)===1){$decimals = strlen($matches[0]) - 1; }else{$decimals = 0; } return number_format($number, $decimals, $dec_point, $thousands_sep);}var_dump(number_format_unchanged_precision(1234.5678, ',', '.'));var_dump(number_format_unchanged_precision(1234.5678, ','));var_dump(number_format_unchanged_precision(12345678));var_dump(number_format_unchanged_precision(-0.5678, ',', '.')); // It occurred a bug with the function in the old comment.?>output is: string(10) "1.234,5678" PHP Warning: 2 parameters for number_format_unchanged_precision() have the same value, that is "," for $dec_point and $thousands_sep in... string(10) "1,234,5678" string(10) "12,345,678" string(7) "-0,5678"

//first remove all white spaces$number=preg_replace('/\s+/', '',$number);//split string into array$numberArr = str_split($number);//reverse array and not preserve key, keys will help to find decimal place$numberArrRev=array_reverse($numberArr);

//find first occurrence of non number character, that will be a decimal place //store $key into variable $decPointIsHere foreach ($numberArrRev as $key => $value) { if(!is_numeric($value)){ if($decPointIsHere==""){$decPointIsHere=$key; } } }

//decimal comma or whatever it is replace with dot //$decPointIsHere is the key of the element that will contain decimal separator dot if($decPointIsHere!=""){$numberArrRev[$decPointIsHere]="."; }

//again check through array for non numerical characters but skipping allready processed keys //if is not number remove from array

I was looking for a SIMPLE way to format currency and account for negative values while not losing the calculation properties of my number. Here's my function - it's not rocket science, but maybe can help someone along the way.

The modification to number_format below does what I actually want it to do, with currency, for example. I want it to show $1.40 and not $1.4 or $1.400 - except in the rare case where I have $1.435 for something, in which case I *don't* want it to round. The ugliness below serves my purpose.

<?php# Function to represent a number like '2nd', '10th', '101st' etcfunction text_number($n){# Array holding the teen numbers. If the last 2 numbers of $n are in this array, then we'll add 'th' to the end of $n$teen_array = array(11, 12, 13, 14, 15, 16, 17, 18, 19);

# Array holding all the single digit numbers. If the last number of $n, or if $n itself, is a key in this array, then we'll add that key's value to the end of $n$single_array = array(1 => 'st', 2 => 'nd', 3 => 'rd', 4 => 'th', 5 => 'th', 6 => 'th', 7 => 'th', 8 => 'th', 9 => 'th', 0 => 'th');

# Store the last 2 digits of $n in order to check if it's a teen number.$if_teen = substr($n, -2, 2);

# Store the last digit of $n in order to check if it's a teen number. If $n is a single digit, $single will simply equal $n.$single = substr($n, -1, 1);

# If $if_teen is in array $teen_array, store $n with 'th' concantenated onto the end of it into $new_nif ( in_array($if_teen, $teen_array) ) {$new_n = $n . 'th'; }# $n is not a teen, so concant the appropriate value of it's $single_array key onto the end of $n and save it into $new_nelseif ( $single_array[$single] ) {$new_n = $n . $single_array[$single]; }

What do you do if some of your numbers have decimal places, and some don't? You can switch between functions, but if you're building it in a loop, that's not a good solution. Instead, we have the same as below, with a slight change:

Some programmers may have scripts that use the number_format function twice on a variable. However, if a number is 4 or more digits, using the function twice with only the decimals parameter will lose part of the value that follows the thousands separator.

Hey Guys, dont know if this is the best way but it worked for me as i faced a problem where I had to split any large number > then 10K into into to exact split amounts.. so i wrote this this to do the trick.. hope it helps some one out there!

People here in India are more used to counting money in Lakhs & Crores .. so here is the code for formatting the commas with thousands for the first time and then with hundred multiples from there after.

Ex: 1234567 -> 12,34,567

<?php

function makecomma($input){// This function is written by some anonymous person - I got it from Googleif(strlen($input)<=2) { return $input; }$length=substr($input,0,strlen($input)-2);$formatted_input = makecomma($length).",".substr($input,-2); return $formatted_input;}

number_format($number,$precision,".","") should be used when setting the value of form elements because if you read the number into a double upon submission, it will only store digits before the comma.
<p>
ie. <input type="text" value="<?php echo(number_format(2.5343,2,".","")">

?>This takes 1.03 seconds all the time. Without the -$start in the number_format cmd, it takes exactly as long (I ran it about 20 times).The simple calculation inside number_format takes relatively no time. Defining the number before entering it into number_format also gives no change:{ $num = microtime(true)-$start; echo number_format($num,25)."\n";}

Which concludes it takes about .01 sec to do a thousand number_format.If you'd do the same routine without the number_format, it'd take .75 seconds -> number_format very fast!

To address the problems number_format has when dealing with big numbers I've created my own Number_Format method, it acts the same way as number_format and takes the same arguments but deals with numbers as strings solving the problems above referred.