PHP Calculation with 0 in front of variable value - php

How to allow PHP calculation 0 appears if example :
$a = "00001";
$b = "00005";
$total = $a + $b;
Expected result is : 00006
Including and keep 0000 appears.
I tried but the result always : 6

Just do normal math and then add the zeros back in front at the end:
$a = "00001";
$b = "00005";
$total = $a + $b;
$total = sprintf("%05d", $total );
or
$total = str_pad($total , 5, "0", STR_PAD_LEFT);

You might want to try and prepend the needed 0's
<?php
$total = 6;
$total_padded = sprintf("%05s", $num);
echo $num_padded; // returns 00006
?>
I haven't tested it but it should work :)

<?php
str_pad($total, 5, '0', STR_PAD_LEFT);
?>

I assume that the value is integer so you should use intval. If it is float use floatval
$a = "00001";
$b = "00005";
print intval($a);
print intval($b);
print intval($a)+intval($b);
than add the leading zeros

<?
str_pad($num, 5 , '0', STR_PAD_LEFT);
?>

Related

add zero after single number if needed

i want to add zero after number if number is single
e.g $a = 2 then 20
if $a = 20 then 20
if $a = 12 then '12'
like this. I some things but it didn't work for me.
How to do it ?
Use if-statements:
if ($a < 10) $a *= 10;
That shouldn't be too hard.
$number = intval($number)
if ($number > 0 && $number < 10) {
$number *= 10;
}
Use str_pad to pad with zeros.
http://php.net/manual/en/function.str-pad.php
Echo str_pad($a, 2, "0", STR_PAD_RIGHT);

Round number in php depending on the value

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

Round small numbers

I would like to know how to achieve this:
round(0.38)
And receive this:
0.40
How can I do this? I tried with round(0.38, 2) but it does not work.
To get 0.4, round to 1 decimal place using round() where the second parameter is the decimal place.
$rounded_number = round(0.38, 1);
To get 0.40, use number_format() to get 2 decimal places.
$formatted_number = number_format( $rounded_number , 2);
Multiply by ten, round, then divide by ten:
php > $a = 0.38;
php > $b = $a * 10;
php > $c = round($b);
php > echo $c/10;
0.4
If you want the two decimal places:
$num = 0.38;
$num = round($num * 10)/10;
$num = number_format($num, 2);
echo $num;
It is probably far simpler than you think it is:
$num = round(0.38 * 10)/10;
echo $num;
Then, you could just replace 0.38 with another number or a variable.
$a = 0.38;
echo sprintf("%.2F", round($a,PHP_ROUND_HALF_UP));

number format in php 999.99 [duplicate]

This question already has answers here:
Print numeric values to two decimal places
(6 answers)
How to pad single-digit numbers with a leading 0
(7 answers)
Closed 11 months ago.
Please help me with the number format in php
for example
I have some calculations, like a+b=c I want answer as 999.99 format in php I've read the number_format,sprintf but its not useful. please give me some useful ideas. My final result should be in 999.99 format.
Per the 'not useful' documentation:
$num = 999.98353;
$num = number_format($num, 2, '.', '');
If you take the time to actually read the documentation, there are pertinent examples.
In addition to number_format, you can use sprintf
$a = 999;
$b = .99;
$c = $a + $b;
echo sprintf('%03.2f', $c); // 999.99
$num = 999.98353;
$num1 = 10;
$num2 = $num + $num1;
echo number_format($num2, 2, '.', ''); // output 1009.98
try this
$a = 500.3755;
$b = 600.9855;
$c = $a + $b;
echo number_format($c, 2, '.', ''); //1101.36
echo number_format($c, 2, '.', ','); //1,101.36
$a = 1234.5678;
$b = 1234.5678;
$num = round(($a + $b), 2);
echo number_format($num, 2, '.', ',');
number_format works, try posting some of your codes.
You can use
$c = sprintf('%0.2f',$a)+sprintf('%0.2f',$b);
Or you can also try :
$c = $a+ $b; // If $c = 999.9999
$c = substr($c, 0, 2); // $c is now the string 999.9999
echo( number_format($c-1,2) ); // Will output 998.99

how do i use 4 digit numbers

I need to count from:
0-9999
In PHP, how can i make the format:
0000-9999
So that the output is:
0000,0001,0002,....
thanks!
$num = 9;
$paddedNum = sprintf("%04d", $num);
echo $paddedNum; // prints 0009
$number = 9;
echo str_pad($number, 4, '0', STR_PAD_LEFT); // output: 0009

Categories