I am having trouble of eliminating .06 using number_format()
what I want to do is:
56.00 => 56
56.10 => 56.10
56.06 => 56
I did it with regex but I can't eliminate it, maybe my statement is wrong.
$num = 50.06;
$num = preg_replace("/\.0*$/",'',$num);
I need your help
Thanks in advance
The regex:
/\.0[0-9]+/
Working DEMO.
So:
$num1 = 50.06;
$num1 = preg_replace("/\.0[0-9]+/",'',$num1); //50
$num2 = 50.10;
$num2 = preg_replace("/\.0[0-9]+/",'',$num2); //50.1
Reference:
preg_replace()
This may be stupid as I'm tired and ready to head out, but:
if(($num - intval($num)) > .1) {
$num = number_format($num, 2);
} else {
$num = number_format($num, 0);
}
The correct regex is \.0[0-9]{1}$
The last param of preg_replace is $num
$num = 56.06;
echo $num = number_format($num, 0);
Related
7000 / 253000 = 0.027667984189723
$number1 = "7000";
$number2 = "253000";
$total = $number1 / $number2;
// return 0.027667984189723
This result how to round 2766 ?
Try this
echo round($total,5);
$number1 / $number2 * 10000 % 1
I'm not sure how you came up with 2766 as a result of your decimal. But I'm up for that challenge. Firstly, to get the correct decimal placing do:
<?php echo round($total, 5); ?>
However, if you're like me, I'm going to figure out that challenge and have some fun with it:
$num1 = round($total, 5);
$num2 = strval($num1);
$num3 = str_split($num2);
$arr1 = array();
foreach ($num3 as $num) {
if(is_string($num)) {
array_push($arr1, $num);
}
}
$arr2 = array_slice($arr1, 3);
foreach($arr2 as $out) {
echo $out;
}
The expected output is 2767, since I did it with the correct round function.
I hope it works for you i tried with different values its working good when value start with 0.
$after_point = ltrim($total,"0."); //27667984189723
echo substr($v,0,4); //2766
if you give 52.548457 then it return 52.5
You must chack that number start with 0.
I know the following code will remove the numbers are the point.
round($number);
I want to round the numbers as follows
if number is 20.123
I want result 20,
If number is 20.567
I want result 21
Means if value is below .5 , it should remove that value.
If value if .5 or above it should round up.
How ?
Anyone help ?
round($number) will do what you want:
round(20.156); // 20
round(20.651); // 21
Live example
I am sure it's working well.
<?php
$var = 22.443;
$var = number_format($var, 0, '.', '');
echo $var;
?>
function get_decimal_num($number){
$num = explode('.', $number);
return $num[1];
}
$num = 10.5 ; // for example
$val = get_decimal_num($num);
if($val >= 5)
{
$value = (int) $num;
echo $value = $value + 1;
}
if($val < 5)
{
echo $num;
}
Plese check it...
Try this:
use the ceil, floor, explode and substr function to achieve you value.
$num = "20.123";
$arr = explode(".", $num);
if(substr($arr[1], 0, 1) >= 5){
$num = ceil($num);
}else{
$num = floor($num);
}
echo $num;
Result:
20
Also you can use the round function.
round(20.156); // 20
round(20.651); // 21
I'm building a counter that counts and displays on a web page the number of images in a certain directory.
The code I'm currently using is this:
<?
$d = opendir("images/myimagefolder");
$count = 0;
$min_digits = 7;
while(($f = readdir($d)) !== false)
if(ereg('.jpg$', $f))
++$count;
closedir($d);
if ($min_digits)
{
$count = sprintf('%0'.$min_digits.'f', $count);
}
$number = $count;
$formattedNumber = sprintf("%07d", $number);
$formattedNumber = str_split($formattedNumber, 3);
$formattedNumber = implode(",", $formattedNumber);
print "$formattedNumber";
?>
This works well and outputs a number like the following: 000,000,5
What I am wanting is to have the separating commas occur every 3 digits from the right not the left, so it would appear as 0,000,005
How would this this be done?
I have tried a number of modifications to my sprintf and str_split code but nothing has worked so far. Any help would be greatly appreciated!
<?php
//image count
$images=count(glob("images/myimagefolder/*.jpg"));
//padding
$images=sprintf("%07s",$images);
//commas
$images=strrev(implode(",",str_split(strrev($images),3)));
//outputs 0,000,005
echo $images;
?>
Had a bit of fun coming up with the shortest possible way to accomplish your solution. :)
$formattedNumber = sprintf("%07d", $number);
$formattedNumber = str_split(strrev($formattedNumber), 3);
for (i=0;i<count($formattedNumber); i++)
$formattedNumber[i] = strrev($formattedNumber[i]);
$formattedNumber = implode(",", array_reverse($formattedNumber));
Drop the last four lines. All you need is 'print number_format ($count);'
http://php.net/manual/en/function.number-format.php
Edit, the above won't work with the leading 0's
I found this in the comments on the php site. A little regex magic should do it in one line.
print preg_replace("/(?<=\d)(?=(\d{3})+(?!\d))/",",",$count);
Here's my take with arrays:
$num = sprintf("%07d", 5);
$digits = str_split($num, 1);
$digits = array_reverse($digits);
$chunks = array_map('array_reverse', array_reverse(array_chunk($digits, 3)));
$concat_chunks = array();
foreach ($chunks as $chunk) {
$concat_chunks[] = join('', $chunk);
}
$output = join(',', $concat_chunks);
print $output;
Alright simple question
I have integriters like
5
188
4634
And they all need to be formated to
0000000005
0000000188
0000004634
It can become a string that doesnt matter.
sprintf is the function for that:
$num = sprintf("%010d", $num);
str_pad
echo str_pad($str, 10, "0",STR_PAD_LEFT);
<?php
#how many chars will be in the string
$filltotal = 10;
$number = 5;
#with str_pad function the zeros will be added
echo str_pad($number, $fill, '0', STR_PAD_LEFT);
// The result: 0000000005
Alternative is str_pad:
echo str_pad($num, 10, "0", STR_PAD_LEFT);
you can achieve by this.
$val = 12;
for($i=0;$i<(10 - count($val));$i++)
{
$str .= '0';
}
$final_val = $str.$val;
How do I split a number by the decimal point in php?
I've got $num = 15/4; which turns $num into 3.75. I would like to split out the 3 and the 75 parts, so $int = 3 and $dec = 75. My non-working code is:
$num = 15/4; // or $num = 3.75;
list($int, $dec) = split('.', $num);
but that results in empty $int and $dec.
Thanks in advance.
If you explode the decimal representation of the number, you lose precision. If you don't mind, so be it (that's ok for textual representation). Take the locale into account! We Belgians use a comma (at least the non-programming ones :).
If you do mind (for computations e.g.), you can use the floor function:
$num = 15/4
$intpart = floor( $num ) // results in 3
$fraction = $num - $intpart // results in 0.75
Note: this is for positive numbers. For negative numbers you can invert the sign, use the positive approach, and reinvert the sign of the int part.
$num = 15/4; // or $num = 3.75;
list($int, $dec) = explode('.', $num);
Try explode
list($int,$dec)=explode('.', $num);
as you don't really need to use a regex based split. Split wasn't working for you as a '.' character would need escaping to provide a literal match.
$int = $num > 0 ? floor($num) : ceil($num);
$dec = $num - $int;
If you want $dec to be positive when $num is negative (like the other answers) you could do:
$dec = abs($num - $int);
$num = 3.75;
$fraction = $num - (int) $num;
In case when you don't want to lose precision, you can use these:
$number = 10.10;
$number = number_format($number, 2, ".", ",");
sscanf($number, '%d.%d', $whole, $fraction);
// you will get $whole = 10, $fraction = 10
This works for positive AND negative numbers:
$num = 5.7;
$whole = (int) $num; // 5
$frac = $num - (int) $num; // .7
$num = 15/4;
substr(strrchr($num, "."), 1)