This question already has answers here:
Zero-pad digits in string
(5 answers)
Number with 0 on the front? [closed]
(2 answers)
Closed 8 years ago.
I am trying to extract part of a number using sustr() but the following is not working:
$num = 012014;
echo substr($num, 0,2);
returns 51
BUT
$num = '012014';
echo substr($num, 0,2);
returns 01
I want it to return 01 can someone help me
Is is not a normal number, when it's prepended with a zero (0). Then it's an octal number
If you treat $num as a string, it'll work.
$num = '012014';
echo substr($num, 0,2);
I guess you must declare the variable as string like this.
$num = '012014';
echo substr( (string)$num, 0, 2 );
Ooops I didn't notice the leading zero. You should just define $num as a string
$num = '0123';
Please try this
$num = 123424;
$num = (string)$num;
echo substr($num, 0,2);
Related
This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 2 years ago.
I am getting issue when increasing number.
For example:
$r = 000123 //Integer
Now i want
$r = 000124
I have try with multiple way.. but i didn't get the result. because of starting with 000.
You can do it this way
$num = "00000123";
// get number without zero prefix
preg_match('/(?!0+)\d+/', $num, $match);
// increase by 1
$match[0]++;
// add zero prefix
$newNum = sprintf('%08d', $match[0]);
var_dump($newNum);
What if you try:
$r = 000123
$r++
This question already has answers here:
How do you pull first 100 characters of a string in PHP
(6 answers)
Closed 10 months ago.
using PHP, How can i remove the rest of character if sting_Len is greater than 6 for example i have 600275L and i want to end up like 600275 but only if greater than 6 digit.
i used the following code to to extract value starts with 600, i want update it to work for the above condition, Thank you
if((substr($key, 0, 3) == "600") && ($row['ItemClass']==3))
{
$assy = $key;
$rout = "Assy";
}
If you always want to limit it to six characters, then you should just be able to use substr for this without checking the length. If you write:
$string = 'abcdefg';
$string = substr($string, 0, 6);
then $string will equal 'abcdef'.
But if $string is shorter than 6 characters, it will just return the entire string. So if you write:
$string = 'abc';
$string = substr($string, 0, 6);
then $string will equal 'abc'.
You can see this in the PHP manual here.
Use the following logic
if(strlen($code) > 6) { echo substr($code, 0, 6); }
This question already has answers here:
Adding leading 0 in php
(3 answers)
Closed 8 years ago.
I have a piece of code for converting a Decimal number into base 3
$number = 10; // For Example
$from_base = 10;
$to_base = 3;
$base_three = base_convert ( $number , $from_base , $to_base );
echo $base_three;
So the number that it echos is 101 and it has 3 digits.
but I what it to echos is 000101 so that it has 6 digits.
Converting Decimal into base 3 with always 6 digits even though it has only 3 or 4 useful digits, is my goal! how can I solve it ?
try this
echo str_pad($base_three, 6, "0", STR_PAD_LEFT);
You can use sprintf to ensure that it always has a total of 6 digits, with leading zeroes:
$base_three = 101;
$padded = sprintf("%06s", $base_three);
echo $padded;
Convert to a string and pad with 0's.
$test = str_pad($base_three, 6, '0', STR_PAD_LEFT);
echo $test;
http://php.net/manual/en/function.str-pad.php
You can use sprintf to make sure you always output 6 digits, whatever number you have:
$number = 010;
sprintf("%06d", $number);
so the complete piece of code would be:
$number = 10; // For Example
$from_base = 10;
$to_base = 3;
$base_three = base_convert ( $number , $from_base , $to_base );
echo sprintf("%06d", $base_three);
or
printf("%06d", $base_three);
printf formats the variable and echos it, sprintf() doesn't echo but returns it
(s)printf can do a lot more, see http://www.php.net/manual/en/function.sprintf.php
This question already has answers here:
Delete digits after two decimal points, without rounding the value
(15 answers)
Closed 8 years ago.
I have a value 3.9609053497942. I need value 3.9 means only one value after decimal. I have used PHP number_format and round functions but it is giving me answer 4.
You could multiply the number by 10, floor() it, and then divide it back.
echo floor($value * 10) / 10;
Try with this,
echo intval((3.9609053497942*10))/10;
or
echo floor((3.9609053497942*10))/10;
There is so many possible solutions:
echo bcadd(3.9609053497942, 0, 1);
preg_match('/\d*\.\d/', 3.9609053497942, $matches);
echo $matches[0];
why not treat it as a string, like
$x = (string)3.96;
$y = explode(".",$x);
$result = $y[0] . "." . $y[1];
Did you tried like:
number_format(3.9609053497942, 1);
This question already has answers here:
Truncate float numbers with PHP
(14 answers)
Closed 9 years ago.
I have a float number in PHP : 0.966666666667
I would like to print it like : 0.96 I used round() and number_format() but they give me both
0.97 is there a function to do that please ?
You can do this:
$num = 0.966666;
$num = floor($num * 100) / 100;
The best way to do this I've found is this:
//$val - the value to truncate
//$dist - the number of digits after to decimal place to keep
function truncate($val, $dist) {
//get position of digit $dist places after decimal point
$pos = strpos($val,'.');
if($pos !== false) {//if $val is actually a float
//get the substring starting at the beginning
//and ending with the point $dist after the
//decimal, inclusive -- convert to float.
$val = floatval(substr($val, 0, $pos + 1 + $dist));
}
return $val;
}
Then just call truncate($YOUR_NUM, 2);.
Source: https://stackoverflow.com/a/12710283/3281590