Change Number string 1000000 to 1.000.000 - php

Using PhP I want to make my numbers more 'readable'...
I want to modify for example:
10 ... 10 (no change)
100 would be 100 (still no change)
1000 would be 1.000
1.000.000 would be 1.000.000
1000000000 would be 1.000.000.000 etc

number_format()

You're looking for NumberFormatter.

$number = 1000000000000.4568;
$str_num = number_format( $number, 2, '.', '.' );
// $str_num Output
1.000.000.000.000.46

Related

How to convert 11,22,33 into 10, 20 and 30 in PHP?

I don't know how to title the question correctly (please tell me what this kind of number called).
I'd like to convert from 2 digits number into a 10 divisible number. For example, I expect:
15 to become 10
23 to become 20
38 to become 30
999 to become 900
9999 to become 9000
I tried searching for the solution on google but I don't know how to type the proper word.
Easy, use the PHP floor function: http://php.net/manual/en/function.floor.php
floor($number/10) * 10
I wrote a simple function should work:
<?php
function roundDown($var){
$len = strlen($var)-1;
$divide = 1;
for($i=1;$i<=$len;$i++){
$divide .= 0;
}
return floor($var/$divide)*$divide;
}
echo roundDown(9999);
Easiest way is divide by ten, then floor value and multiply by ten. (Floor method - phpdocs)
floor($number/10)*10
Use round:
echo round(1241757, -6); // 1000000
from http://php.net/manual/en/function.round.php
You are looking for the "one significant figure".
I think this answer provides a good solution:
How to round down to the nearest significant figure in php
Short form:
$x = $y - $y % pow(10, floor(log10($y)));

Formatting number to decimal

I understand how to format a number from for example 2.10 to 2.1 nut how would i format a number so that 381 -> 38.1% or 38.1
Either result is fine as I can just add a percent sign after the first result. I was thinking of maybe splitting the number then adding a decimal after the second number but this wouldn't work with 100%?
If 381 is a percentage of 1000, then 381 divided by 1000 = 0.381
Multiply 0.381 by 100 to get 38.1
The SQL CODE would be:
DECLARE #Number1 Decimal(18,1) = 381, #Number2 Decimal(18,1) = 0
SET #Number2 = CAST(ROUND((#Number1 / 1000) *(100),1,1) AS DECIMAL(18,1))
SELECT #Number2
Hope this helps.
This should work for you:
$number = 381;
echo sprintf("%02.2f%%", $number/10);
Output:
38.10%
If you don't want it with 2 decimal just change .2 as you want it
Doing $number = $number / 10; should turn the trick.

Formatting numbers from 1000 to 10.00

I'll like to format 1000 to 10.00
The PHP number_format function does not seem to be working for this.
I have tried:
$amount2 = number_format("$cost",2,"",",");
echo "$cost";
Any ideas? Is there a way I can manupulate number_format to display the results (i.e just inserting a decimal before the last two digits?
Number format will change the "." to a "," but you telling it to format ONE THOUSAND.
$cost=1000;
echo number_format($cost,2,'.',',');
//1,000.00
What you want is simply:
$cost=1000;
echo number_format($cost/100,2,'.',',');
//10.00
Is this legit for you ?
<?php
$cost=1000;
echo substr($cost, 0, 2) . "." . substr($cost, 2);//10.00
1000 and 10.00 are totally different numbers (in values). Divide by 100, then format it properly:
$cost = 1000 ;
$cost /= 100 ;
$amount2 = number_format($cost,2,".","");
echo $amount2 ;
Try this code:
$stringA= 1000;
$length=strlen($stringA);
$temp1=substr($stringA,0,$length-2);
$temp2=substr($stringA,$length-2,$length);
echo $temp1.".".$temp2; // Displays 10.00
The third parameter to number_format should be the character you want to use as a decimal point. Why are you passing an empty string? And why are you placing your number ($cost) inside a string?
Try this: echo number_format($cost,2,'.',',');
EDIT: Perhaps I misunderstood your question — if you want the number 1000 to be displayed as 10.00, just divide $cost by 100 before calling number_format().

How to keep two digits in float number (PHP)

How to make number like 3.0000000054978E+38 to 3.00 in PHP?
Many thanks!
You cannot use round to solve this since it is a number in scientific notation. You can, however, use substr:
$i = 3.0000000054978E+38;
$i = substr($i, 0, 2); // $i is now the string 3.00
echo( number_format($i+1,2) ); // Will output 4.00
In case you are looking for the small fraction of your number being outputted in a formatted fashion:
$number = 3.0000000054978E+38;
printf('%.2f', $number / 1E+38); # 3.00
You can just use round, as in round($floating_number, 2).
sprintf() always gives you the specified number of decimal points, if you require.
sprintf('%0.2f', 3.0000000);
Would display 3.00, if you echo it.

Finding factor for position

I know question title seems quite 'un-understandable', but I don't know how to write question title for this particular question.
Question:
I want to find factor for position.
Let me clear you with an example.
Value Factor
[Available] [Have to find out]
----------------------------------
1 10
3 10
9 10
10 10
11 10
25 10
50 10
75 10
99 10
100 100
101 100
105 100
111 100
127 100
389 100
692 100
905 100
999 100
1000 1000
1099 1000
1111 1000
4500 1000
6825 1000
7789 1000
9999 1000
10000 10000
10099 10000
51234 10000
98524 10000
99999 10000
100000 100000
and so on.
I hope you understand what I mean to get.
Assuming that the first three values should be 1 (as noted by Asaph), then you just need to use all that logarithm stuff you learned in school:
pow(10, floor(log10($n)))
So, how does this work? The base-10 logarithm of a number x is the y such that 10^y = x (where ^ stands for exponentiation). This gives us the following:
log( 1) 0
log( 10) 1
log(100) 2
...
So the log10 of a number between 1 and 10 will be between 0 and 1, the log10 of a number between 10 and 100 will be between 1 and 2, etc. The floor function will give you the integer part of the logarithm (we're only dealing with non-negative values here so there's no need to worry about which direction floor goes with negative values) so floor(log10()) will be 0 for for anything between 1 and 10, 1 for anything between 10 and 100, etc. Now we have how many factors of ten we need so a simple pow(10, ...) gives us the final result.
References:
log10
floor
pow
I'm still a little unsure of what you're asking, but it seems like you want to map values to other values... In php arrays can be indexed with anything (making them a map). If 999 always means a factor of 100 and 1099 always means a factor of 1000, you can set the value of array[999] to 100 and the value of array[1099] to 1000, etc.
Basically Factor is 10 to the power of number of digits in $value minus 1 (except for the single digit numbers):
if($value < 10) {
$value += 10;
}
$numOfDigits = count(str_split($value,1));
$factor = pow(10,$numDigits-1);
This function should work for you. It seems like the first 3 "factors" in your list don't fit the pattern. If, in your sample data set, those first 3 "factors" should really be 1 instead of 10, then you can safely remove the first 3 lines of the body of the function below.
function getFactor($num) {
if ($num < 10) { // If the first 3 "factors" listed
return 10; // in the question should be 1 instead of 10
} // then remove these 3 lines.
$factor = 1;
while($factor <= $num) {
$factor *= 10;
}
return $factor / 10;
}

Categories