This question already has answers here:
How to pad single-digit numbers with a leading 0
(7 answers)
Closed 11 months ago.
I have the value as $title =10 and I want to minus it with 01.So my code is:
echo $minus = round(($title - 01),2);
I want the result $minus = 09 but this code not like this it still have the result $minus=9. Anyone help me please,Thanks.
The problem is that PHP is not strongly typed, and round() returns a number, which automatically has the leading zero stripped. Try:
$minus = "0" . round(($title - 01),2);
PHP is evaluating your 0-prefixed numbers to their base value -- 04 and 01 are 4 and 1 respectively.
If you want them to be output with a leading 0, try using a number formatter, or string padding or simply append them to the string, "0"
What's happening is that round() returns an integer. Which means it won't have any 0's before it. If you want it to return 0's before it, try
str_pad(round(($title - 1), 2), 2, "0");
That will make it always append a 0 before the number if it's only 1 number, but if it's 10 or 15 or something, it won't append a 0
echo str_pad(intval($title) - 1, 2, "0", STR_PAD_LEFT);
This will pad your result with a 0 if the result is only one digit; otherwise, it will not pad. For a leading zero always, you can replace the 2 with strlen($title)
Try this..
$i=04-01;
echo sprintf('%02s', $i);
Related
This question already has answers here:
How to make number_format() not to round numbers up
(18 answers)
Closed 3 years ago.
I have this number
$sku = '2200081005966';
and I want to convert the number like this without rounding the number
$new_sku = '5.96'
I already try this number_format(substr($sku, 7, 12), 0, '', '.')
but the output that I get 5.97.
Any ideas how can I make this work?
Thank you.
Add floor() around your number_format:
$sku = '2200081005966';
echo floor(number_format(substr($sku, 7, 12), 0, '', '.')*100)/100;
Outputs:
5.96
Note: It would works well in case of positive numbers, your substr always take a positive number, that's why it would be enough.
<?php
$sku = '2200081005966';
$foo=substr($sku, 9, 12);
echo substr($foo, 0,2).".".substr($foo, 3,3);
?>
first you get the last 4 charakters of the string, then you split that part and put a dot between.
This question already has an answer here:
Strange behaviour with numbers that have a leading zero [duplicate]
(1 answer)
Closed 5 years ago.
echo (int)01; //1
echo (int)02; //2
echo (int)03; //3
echo (int)04; //4
echo (int)05; //5
echo (int)06; //6
echo (int)07; //7
echo (int)08; //0
echo (int)09; //0
echo (int)010; //8
echo (int)011; //9
echo (int)012; //10
echo (int)013; //11
(int) was doing right from 01 to 07. But after that it goes wrong. What's the reason of it??
Perhaps 08 is expected to be octal number, like 0x is hexadecimal.
If a number starts with 0 it is consider an octal number
and since these numbers range from 0 to 7 only. You get a 0 since 8%8 = 0
Reference: http://php.net/manual/en/language.types.integer.php
A side note on number systems
Have you wondered why the next number to 9 is 10? and why in the binary system the sequence is 0, 1, 10, 11... ? And why the octal system allows only values from 0 to 7?
It is because number systems usually increment values based on modulo logic on the base.
For example take the binary number system. Since it is base 2, it can only contain values 0, 1 since 0%2 = 0, 1%2 = 1, but 2%2 is again 0
So whenever a greater number comes, say 3. Its value in binary is (increment by one in the preceding place) (put the modulo in the existing place)
So the value of 3 in the binary system is (0+1) (3%2) = 11
Though this is not the exact logic, just putting it here for a beginner reference
This question already has answers here:
Adding leading 0 in php
(3 answers)
Closed 8 years ago.
Hi all just a very quick one.
How would I write in an sql query or php to change the number of digits displayed.
For example I am using this $values['ClientId'] which is a AI primary key, I know that until I get to 10 it will look like 1, 2, 3, 4,...,10, but I want it to look like 01, 02, 03. or even 001.
Probably a real simple one but I c
ant find it.
Use the str_pad function:
http://php.net/manual/en/function.str-pad.php
str_pad( $number, $padLength, $padWith, STR_PAD_LEFT );
str_pad( "1", 4, "0", STR_PAD_LEFT ); // gives 0001
$input=1; //if you need 01 instated of 1 then try
echo sprintf("%02d", $input);
$input=1; //if you need 001 instated of 1 then try
echo sprintf("%03d", $input);
please read this sprintf
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: show a number to 2 decimal places
How can I format an input number to be 0.00 if it has not any value? I tried (double) but it prints 0 only.
Here you go :)
echo number_format($var,2);
If you want it to print specific no. of decimal points, use number_format.
$float_var = number_format($var, 2);
$var = number_format($number, 2, '.', '');
This forces 2 points after the decimal, sets the decimal as a period. You can also forego the last two as it defaults to it;
Note: The third value is your decimal separator, the fourth value is the thousandths separator.
$var = number_format($number, 2);
For direct output:
printf('%0.2f',$var);
Output into variable:
$outVar = sprintf('%0.2f',$var);
This statemant casts $var type to float and prints with 2 decimal signs
maybe you should check it first if the value is not set
if(!isset($variableName))
{
// then set
$variableName = "0.00"; // => string
//or like this
$variableName = number_format(0,2); // => this result is also string
}
echo "value: ",$variableName;
result
0.00
if you are trying to format the value i sugest you to use meioMask pluging.
So you define your field as number and the pluging do the trick, even if you set "0" for the value
This:
$difference = 05-1;
results in
4
same if I do this:
$difference = 05-01;
is there a inbuilt way to subtract while keeping leading zeros? I know I can check to see if the difference has only 1 character or not and if so add a leading zero but was just wondering if there is a default way to get the result back with 0 already in it.
No I dont think PHP will natively keep the leading 0's unless its a float. In PHPs mind 4 is 4 not 04 tho 0.4 is 0.4
So if you need the leading 0 in ints lower the 10 pad it with str_pad():
<?php
$difference = (05-1);
echo str_pad($difference, 2, "0", STR_PAD_LEFT);//04
?>
<?php
$difference = 234;
echo str_pad($difference, 2, "0", STR_PAD_LEFT);//234
?>
If it's just a matter of outputting you can use printf() to add leading zeroes. The following:
<?php
printf("Result: %02d", 04-1);
?>
will output:
Result: 03
the %02d translates to fill with '0' (%0 2d) for 2 spaces (%0 2 d) and format as an integer (%02 d). A lot can be done with printf() to set precision, add leading characters, and use placeholders while outputting text.