PHP automatically removes the last zero after the point, Why? - php

PHP automatically removes the last zero after the decimal point. Forinstance
$number=29.10;
$echo 'Number='. $number;
This will print
Number = 29.1
instead of
Number = 29.10
I actually need the zero after the decimal place but to no avail.
Why is this so? Any idea or link about this, is very much appreciated.
Thanks

Related

last zeros gets stripped when use php minus calculation

I'm using minus php query to calculate and find out two variables difference. Here is the code i'm using to calculate
Example
$v1="13.240";
$v2="0";
echo $v1 - $v2;
When calculation gets completed, the zero at the end of variable gets stripped eg (13.24), which in right scenario should have been 13.240. I do not understand how to fix this issue.
Both are same, but if you need that badly, then you can use number_format() to keep number 3 decimal places,
<?php
$v1="13.240";
$v2="0";
echo number_format($v1 - $v2, 3);
?>
DEMO: https://3v4l.org/FV1Or

inval return wrong number if the number start with zero

I have a number 00101 when I print out this number (or using it for my purpose) I got 65, I've tried intval() but it also returns 65. Can anyone explain to me why? And what is the easiest way to get 00101, or 101?
I would say you are using an invalid type of number, there is bits type (see the list of types in https://www.php.net/manual/en/langref.php)
if you run the following
$a = array(10101, 11100, 11010, 00101);
var_dump($a);
you will see that PHP convert your number to int 65
so maybe you want to use strings?
You will get 101 from string '00101' when passing to intval function. However an integer number does not have start with leading 0; PHP does not get it as decimal number.

replace characters in string until different character

OK lets assume I have
$value = '0.0005304';
I'm struggling to work out how I can remove all instances of 0
until another character is detected.
So in the case above the end result would be 5304
I know I should show code working out however I'm unsure on how to do this so don't know where to start looking.
Any help would be appreciated
** Added **
The 2 numbers are stock market prices , open & close
basically I want to deduct the open from the close
this value will be the difference between the 2
example open = 1.23456 close = 1.23446 is a difference of -0.00010
I would want this represented as -10 points
Hope this explains more
Try the following:
$value = '0.0005304';
$value = (int)str_replace('.','', $value);
var_dump($value); //output 5304
str_replace will remove the . and (int) will cast it to int which will remove all the 0s
Trim off the zeroes, or in your case, the zeroes and the decimal.
$value = ltrim($value, '0.');
I don't know if referring to this as "replace characters" is accurate, though. It seems we're replacing them with nothing.

issue with adding decimal point in front of an int

So I am trying to add a decimal point in front of a whole in (10) The issue is I'm trying to use sprintf(); but it seems to not be working for me :(.
Code:
sprintf(".",($percentamount));
Let me know what I am doing wrong thanks!
I have already tried "%.f"
if i understand you correctly you will take $percentamount variable which have to contain 10 as its value and just put '.' in front of it to make string '0.1' out of it - is it right? If yes than you could take 10 as digit and place it within a string as follows:
sprintf('.%d', 10); // %d stands for digit and means digit should be your second parameter
if you want to have 10 as string input you could write it as follows:
sprintf('.%s', '10');
if it is not the solution you looking for please describe it better and i'll try to help

PHP ceil gives wrong results if input is a float with no decimal

I've been wrestling with PHP's ceil() function giving me slightly wrong results - consider the following:
$num = 2.7*3; //float(8.1)
$num*=10; //float(81)
$num = ceil($num); //82, but shouldn't this be 81??
$num/=10; //float(8.2)
I have a number which may have any number of decimal places, and I need it rounded up to one decimal place.
i.e 8.1 should be 8.1, 8.154 should be 8.2, and 8 should be left as 8.
How I've been getting there is to take the number, multiply by 10, ceil() it, then divide by ten but as you can see I'm getting an extra .1 added in some circumstances.
Can anyone tell my why this is happening, and how to fix it?
Any help greatly appreciated
EDIT: had +=10 instead of *=10 :S
EDIT 2:
I didn't explicitly mention this but I need the decimal to ALWAYS round UP, never down - this answer is closest so far:
rtrim(rtrim(sprintf('%.1f', $num), '0'), '.');
However rounds 3.84 down to 3.8 when I need 3.9.
Sorry this wasn't clearer :(
Final Edit:
What I ended up doing was this:
$num = 2.7*3; //float(8.1)
$num*=10; //float(81)
$num = ceil(round($num, 2)); //81 :)
$num/=10; //float(8.1)
Which works :)
This is more than likely due to floating point error.
http://support.microsoft.com/kb/42980
http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html
http://joshblog.net/2007/01/30/flash-floating-point-number-errors/
http://en.wikipedia.org/wiki/Floating_point
You may have luck trying this procedure instead.
<?php
$num = 2.7*3;
echo rtrim(rtrim(sprintf('%.1f', $num), '0'), '.');
Floats can be a fickle thing. Not all real numbers can be properly represented in a finite number of binary bits.
As it turns out, a decimal section of 0.7 is one of those numbers (comes out 0.10 with an infinity repeating "1100" after it). You end up with a number that's ever so slightly above 0.7, so when you multiply by 10, you have a one's digit slightly above 7.
What you can do is make a sanity check. Take you float digit and subtract it's integer form. If the resulting value is less than, say, 0.0001, consider it to be an internal rounding error and leave it as-is. If the result is greater than 0.0001, apply ceil() normally.
Edit: A fun example you can do if you're on windows to show this is to open up the built in calculator application. Put in "4" then apply a square root function (with x^y where y=0.5). You'll see it properly displays "2". Now, subtract 2 from it and you'll see that you don't have 0 as a result. This is caused by internal rounding errors when it attempted to compute the square root of 4. When displaying the number 2 earlier, it knew that those very distant trailing digits were probably a rounding error, but when those are all that's left, it gets a bit confused.
(Before anybody gets onto me about this, I understand that this is oversimplified, but nonetheless I consider it a decent example.)
Convert your number to a string and ceil the string.
function roundUp($number, $decimalPlaces){
$multi = pow(10, $decimalPlaces);
$nrAsStr = ($number * $multi) . "";
return ceil($nrAsStr) / $multi;
}
The problem is that floating point numbers are RARELY what you expect them to be. Your 2.7*3 is probably coming out to be something like 81.0000000000000000001, which ceil()'s up to 82. For this sort of thing, you'll have to wrap your ceil/round/floor calls with some precision checks, to handle those extra microscopic differences.
Use %f instead of %.1f.
echo rtrim(rtrim(sprintf('%f', $num), '0'), '.');
Why not try this:
$num = 2.7*3;
$num *= 100;
$num = floor($num);
$num /= 10;
$num = ceil($num);
$num /= 10;

Categories