Division remainder return 2 zero [duplicate] - php

This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 4 years ago.
Modulus (%) operator return single zero. It is correct but i want to 2 zero after using modulus (%) operator. I don't want to use if condition.
$k = 60;
$result = $k%60;
echo "Result = ".$result;
Output
Result = 0
Expected output
Result = 00

try it :
$k = 60;
$result = $k%60;
echo str_pad($result, 2, '0', STR_PAD_LEFT);
or test this method :
$result=0
$pad_length = 2;
$pad_char = 0;
$str_type = 'd'; // treats input as integer, and outputs as a (signed)
decimal number
$format = "%{$pad_char}{$pad_length}{$str_type}";
// output and echo
printf($format, $result);

Related

Can not get proper result for type casting in PHP [duplicate]

This question already has answers here:
How to convert float value to integer in php?
(6 answers)
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I have a Float variable
$float = 37.80
Then I multiply the float by 100:
$mul = $float *100
Now I have 3780.
$typecasting = (int) $mul;
Now when I print $typecasting, it shows 3779
For example:
$float = 37.80;
$mul = $float*100;
echo $mul;
echo '<br>';
//$mul = 3780
$typecasting = (int) $mul;
echo $typecasting;
// $typecasting shows 3779
I can't understand, how it works.
This problem occurs only for 32.80, 33.80, ...4.80. and also for multiples of 2, i.e. 65.60.
Just use floatval() http://php.net/manual/en/function.floatval.php
$float = 37.80;
$mul = $float*100;
echo $mul;
echo '<br>';
//$mul = 3780
$typecasting = floatval($mul);
echo $typecasting;
// $typecasting shows 3779

Loop Removes Zero Before Integer [duplicate]

This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 6 years ago.
If my loop runs 3 times, then the output is 0001,2,3, but I need
0001
0002
0003
How do I get this output?
$i='0001';
foreach($test as $rows){
echo $i;
echo '<br/>';
$i++;
}
Use printf with a padding specifier, e.g.:
$test = range(1,11);
$i = 0;
foreach ($test as $rows) {
printf("%04d<br/>\n", ++$i);
}
Output
0001<br/>
0002<br/>
0003<br/>
0004<br/>
0005<br/>
0006<br/>
0007<br/>
0008<br/>
0009<br/>
0010<br/>
0011<br/>
In this example, 04 is a padding specifier meaning that the number (d) is padded with maximum 4 zeroes.
You can use str_pad()
$i = "0001";
for($j=0;$j<1000;$j++) {
echo str_pad($i++, 4, '0', STR_PAD_LEFT);
echo "<br/>";
}
You can cheat a little bit,
$i = "1";
$y = "000"; //Added this
while ($i < 4){
echo $y . $i;
$i++;
}
Basically you echo the 000 in front of the number each time.

Removing trailing zero's from decimal [duplicate]

This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 8 years ago.
Hi i was wondering how i could remove the last 2 zero's as seen below:
13.6500
16.0000
17.5345
To the following:
13.65
16.00
17.5345
You can use round() like this to round to 2 decimal points like this
echo round("13.6500",2);
Also you can use number_format() like this
echo number_format("13.6500", 2);
Both yeilds the O/P
13.65
Demo
EDIT
from the comment i think what you want is like this
$input = "12.00000";
$length = strlen(substr(strrchr($input, "."), 1));
if($length > 2)
{
$input =$input+0;
if(strlen(substr(strrchr($input, "."), 1)) < 1)
{
$input = $input.".00";
}
else if(strlen(substr(strrchr($input, "."), 1)) < 2)
{
$input = $input.".0";
}
echo $input;
}
else
{
echo $input;
}
Demo

02 (str) - 1 (int) = 01 (str) How to get result like this? [duplicate]

This question already has answers here:
How to pad single-digit numbers with a leading 0
(7 answers)
Closed 11 months ago.
<?php
$str = "02";
$int = 1;
$result = $str-$int;
echo $result // 1
?>
But I need result = 01
Don't tell me "0".$str-$int;
<?php
$str = "02";
$int = 1;
printf('%02d', $str-$int);
or
<?php
$str = "02";
$int = 1;
$result = sprintf('%02d', $str-$int);
// do something with $result here
see http://docs.php.net/manual/en/function.sprintf.php
try
printf("%02d",$str-$int);
The explanation of the numerous formatting possibilities of printf are explained in the sprintf docs
<?
$str = "02";
$int = 1;
echo sprintf("%02d", (int)$str - $int);
?>
Use str_pad() where you set it to pad the left with 0's until the length of string is 2 chars.
echo str_pad($str - $int, 2, '0', STR_PAD_LEFT);

perform round function without using round() in php [duplicate]

This question already has answers here:
Print numeric values to two decimal places
(6 answers)
Closed 11 months ago.
without use of round() function perfrom the round() in php
$a = "123.45785";
$v = round($a);
output: 123.46;
it had done by round function but i want to get output without use of round and number_format() function.
Here's a way of doing it with arithmetics:
function my_round($num, $places = 2) {
// Multiply to "move" decimals to the integer part
// (Save one extra digit for rounding)
$num *= pow(10, $places + 1);
// Truncate to remove decimal part
$num = (int) $num;
// Do rounding based on the last digit
$lastDigit = $num % 10;
if ($lastDigit >= 5)
$num += 10;
// Remove last digit
$num = (int) ($num/10);
// "Move" decimals in place, and you're done
$num /= pow(10, $places);
return $num;
}
You have sprintf.
$a = "123.45785";
echo sprintf("%01.2f", $a); // output: 123.46

Categories