number format in php 999.99 [duplicate] - php

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

Related

PHP - For Loop Keep Significant Digits [duplicate]

This question already has answers here:
Zero-pad digits in string
(5 answers)
Closed 2 years ago.
I have a variable which contains the value 1234567.
I would like it to contain exactly 8 digits, i.e. 01234567.
Is there a PHP function for that?
Use sprintf :
sprintf('%08d', 1234567);
Alternatively you can also use str_pad:
str_pad($value, 8, '0', STR_PAD_LEFT);
Given that the value is in $value:
To echo it:
printf("%08d", $value);
To get it:
$formatted_value = sprintf("%08d", $value);
That should do the trick
When I need 01 instead of 1, the following worked for me:
$number = 1;
$number = str_pad($number, 2, '0', STR_PAD_LEFT);
echo str_pad("1234567", 8, '0', STR_PAD_LEFT);
sprintf is what you need.
EDIT (somehow requested by the downvotes), from the page linked above, here's a sample "zero-padded integers":
<?php
$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);
?>
Though I'm not really sure what you want to do you are probably looking for sprintf.
This would be:
$value = sprintf( '%08d', 1234567 );
Simple answer
$p = 1234567;
$p = sprintf("%08d",$p);
I'm not sure how to interpret the comment saying "It will never be more than 8 digits" and if it's referring to the input or the output. If it refers to the output you would have to have an additional substr() call to clip the string.
To clip the first 8 digits
$p = substr(sprintf('%08d', $p),0,8);
To clip the last 8 digits
$p = substr(sprintf('%08d', $p),-8,8);
If the input numbers have always 7 or 8 digits, you can also use
$str = ($input < 10000000) ? 0 . $input : $input;
I ran some tests and get that this would be up to double as fast as str_pad or sprintf.
If the input can have any length, then you could also use
$str = substr('00000000' . $input, -8);
This is not as fast as the other one, but should also be a little bit faster than str_pad and sprintf.
Btw: My test also said that sprintf is a little faster than str_pad. I made all tests with PHP 5.6.
Edit: Altough the substr version seems to be still very fast (PHP 7.2), it also is broken in case your input can be longer than the length you want to pad to. E.g. you want to pad to 3 digits and your input has 4 than substr('0000' . '1234', -3) = '234' will only result in the last 3 digits
$no_of_digit = 10;
$number = 123;
$length = strlen((string)$number);
for($i = $length;$i<$no_of_digit;$i++)
{
$number = '0'.$number;
}
echo $number; /////// result 0000000123
I wrote this simple function to produce this format: 01:00:03
Seconds are always shown (even if zero).
Minutes are shown if greater than zero or if hours or days are required.
Hours are shown if greater than zero or if days are required.
Days are shown if greater than zero.
function formatSeconds($secs) {
$result = '';
$seconds = intval($secs) % 60;
$minutes = (intval($secs) / 60) % 60;
$hours = (intval($secs) / 3600) % 24;
$days = intval(intval($secs) / (3600*24));
if ($days > 0) {
$result = str_pad($days, 2, '0', STR_PAD_LEFT) . ':';
}
if(($hours > 0) || ($result!="")) {
$result .= str_pad($hours, 2, '0', STR_PAD_LEFT) . ':';
}
if (($minutes > 0) || ($result!="")) {
$result .= str_pad($minutes, 2, '0', STR_PAD_LEFT) . ':';
}
//seconds aways shown
$result .= str_pad($seconds, 2, '0', STR_PAD_LEFT);
return $result;
} //funct
Examples:
echo formatSeconds(15); //15
echo formatSeconds(100); //01:40
echo formatSeconds(10800); //03:00:00 (mins shown even if zero)
echo formatSeconds(10000000); //115:17:46:40
You can always abuse type juggling:
function zpad(int $value, int $pad): string {
return substr(1, $value + 10 ** $pad);
}
This wont work as expected if either 10 ** pad > INT_MAX or value >= 10 * pad.

PHP Calculation with 0 in front of variable value

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);
?>

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);

Covert digit to x.xx format in php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
simple regex in php, formatting decimal number
How to convert a digit in x format to x.xx format in php?
example :
I have a number 5. Need to convert to 5.00
$n = 5;
$n = number_format($n, 2, '.', '');
You should try number_format():
$your_number = 5;
echo number_format($your_number, 2); // displays 5.00
Manual
See http://php.net/manual/en/function.number-format.php
number_format('your digit', 2 or 1, '.', '');
$number = 125
$no = number_format($number, 2, '.', '');
output = 125.00
$x = 5;
$x = sprintf('%.2f', $x);
echo $x;

Formatting a number with leading zeros in PHP [duplicate]

This question already has answers here:
Zero-pad digits in string
(5 answers)
Closed 2 years ago.
I have a variable which contains the value 1234567.
I would like it to contain exactly 8 digits, i.e. 01234567.
Is there a PHP function for that?
Use sprintf :
sprintf('%08d', 1234567);
Alternatively you can also use str_pad:
str_pad($value, 8, '0', STR_PAD_LEFT);
Given that the value is in $value:
To echo it:
printf("%08d", $value);
To get it:
$formatted_value = sprintf("%08d", $value);
That should do the trick
When I need 01 instead of 1, the following worked for me:
$number = 1;
$number = str_pad($number, 2, '0', STR_PAD_LEFT);
echo str_pad("1234567", 8, '0', STR_PAD_LEFT);
sprintf is what you need.
EDIT (somehow requested by the downvotes), from the page linked above, here's a sample "zero-padded integers":
<?php
$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);
?>
Though I'm not really sure what you want to do you are probably looking for sprintf.
This would be:
$value = sprintf( '%08d', 1234567 );
Simple answer
$p = 1234567;
$p = sprintf("%08d",$p);
I'm not sure how to interpret the comment saying "It will never be more than 8 digits" and if it's referring to the input or the output. If it refers to the output you would have to have an additional substr() call to clip the string.
To clip the first 8 digits
$p = substr(sprintf('%08d', $p),0,8);
To clip the last 8 digits
$p = substr(sprintf('%08d', $p),-8,8);
If the input numbers have always 7 or 8 digits, you can also use
$str = ($input < 10000000) ? 0 . $input : $input;
I ran some tests and get that this would be up to double as fast as str_pad or sprintf.
If the input can have any length, then you could also use
$str = substr('00000000' . $input, -8);
This is not as fast as the other one, but should also be a little bit faster than str_pad and sprintf.
Btw: My test also said that sprintf is a little faster than str_pad. I made all tests with PHP 5.6.
Edit: Altough the substr version seems to be still very fast (PHP 7.2), it also is broken in case your input can be longer than the length you want to pad to. E.g. you want to pad to 3 digits and your input has 4 than substr('0000' . '1234', -3) = '234' will only result in the last 3 digits
$no_of_digit = 10;
$number = 123;
$length = strlen((string)$number);
for($i = $length;$i<$no_of_digit;$i++)
{
$number = '0'.$number;
}
echo $number; /////// result 0000000123
I wrote this simple function to produce this format: 01:00:03
Seconds are always shown (even if zero).
Minutes are shown if greater than zero or if hours or days are required.
Hours are shown if greater than zero or if days are required.
Days are shown if greater than zero.
function formatSeconds($secs) {
$result = '';
$seconds = intval($secs) % 60;
$minutes = (intval($secs) / 60) % 60;
$hours = (intval($secs) / 3600) % 24;
$days = intval(intval($secs) / (3600*24));
if ($days > 0) {
$result = str_pad($days, 2, '0', STR_PAD_LEFT) . ':';
}
if(($hours > 0) || ($result!="")) {
$result .= str_pad($hours, 2, '0', STR_PAD_LEFT) . ':';
}
if (($minutes > 0) || ($result!="")) {
$result .= str_pad($minutes, 2, '0', STR_PAD_LEFT) . ':';
}
//seconds aways shown
$result .= str_pad($seconds, 2, '0', STR_PAD_LEFT);
return $result;
} //funct
Examples:
echo formatSeconds(15); //15
echo formatSeconds(100); //01:40
echo formatSeconds(10800); //03:00:00 (mins shown even if zero)
echo formatSeconds(10000000); //115:17:46:40
You can always abuse type juggling:
function zpad(int $value, int $pad): string {
return substr(1, $value + 10 ** $pad);
}
This wont work as expected if either 10 ** pad > INT_MAX or value >= 10 * pad.

Categories