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
Related
This question already has answers here:
Break long string into pieces php
(5 answers)
Closed 3 years ago.
I want to make a program which splits a long number into pieces of 13 digited number such that I can loop for every 13 digits just using php.
$number = 012345678901230123456789123
Should output
0123456789123
0123456789123
And it should be for any large number having the number of digit multiple of 13.It looks about looping and algorithm but I want to make it as short as possible and I have doubts on how to do it. So I am just asking about the main concept.
The most dynamic solution is probably to use array_functions on the string.
So str_split to make it array then chunk it in size 13 and implode the arrays.
$number = "012345678901230123456789123";
$arr = array_chunk(str_split($number), 13);
foreach($arr as &$val){
$val = implode($val);
}
https://3v4l.org/LsNFt
You can create a function where you can use your string and size as parameter and return an array of strings of the desired length:
function splitString($str, $packetSize) {
$output = [];
$size = strlen($str);
for ($i = 0; $i < $size; $i += $packetSize) {
if ($i + $packetSize < $size) {
$output[]= substr($str, $i, $packetSize);
} else {
$output[]=substr($str, $i);
}
}
return $output;
}
This question already has answers here:
Round to max thousand, hundred etc in PHP
(5 answers)
Closed 5 years ago.
PHP's floor() and ceil() is useful for rounding floats to their nearest integers.
But what if we need to find the highest multiple of 10 (hundreds, thousands, etc.) for a given number? For example:
num_to_scale(538.9) // 500
num_to_scale(543123654.01234) // 5000000
Try this code, check the live demo. You also can refer to this post.
$floor = floor($v);
return str_pad(substr($floor, 0, 1), strlen($floor), '0');
function num_to_scale($number) {
$multiple=1; $test=1;
for ($i=1; $test>=1; $i++) {
$factor = $multiple/10;
$test = $number/$multiple;
$multiple = $multiple*10;
$scale = floor($number/$factor)*$factor;
} return $scale;
}
or more simply:
function num_to_scale($n) {
$m=1; $t=1;
for ($i=1; $t>=1; $i++) {
$f=$m/10; $t=$n/$m; $m=$m*10; $s=floor($n/$f)*$f;
} return $s;
}
Combined with a helper function to convert integers to text strings, we can easily do something like this:
echo 'Over '.num_to_scale(5395206).' units sold.';
// Over five million units sold.
try this:
function num_to_scale($number) {
$num = (int) floor($number);
$ln = strlen($num) - 1;
return (int) floor($num / pow(10, $ln)) * pow(10, $ln);
}
My answer uses logarithm :
function num_to_scale($var) {
$nbDigits = (int)log($var,10);
return (int)($var/10**$nbDigits)*10**$nbDigits;
}
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.
This question already has answers here:
Generating UNIQUE Random Numbers within a range
(14 answers)
Closed 7 years ago.
I am trying to find a solution in PHP that can generate three random numbers.
At the moment I have this that generates a random number that is different from $randNum;
The numbers need to be different from each other and also different from the variable $randNum
Thank you
$wrong = $randNum;
while ($wrong == $randNum) {
$wrong = rand(0,$max - 1);
}
<?php
$numbers = [];
for($i = 0; $i < 10; $i++){
$number = rand (1,15);
while (in_array($number, $numbers)){
$number = rand (1,15);
}
$numbers[] = $number;
}
echo '<pre>';
print_r($numbers);
echo '</pre>';
This function generates unique 10 random numbers, range from 1-15 you can easy change this script to your needs.
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