php session echo intval with decimals - php

Hello now my code looks like:
echo number_format($_SESSION['price_summ']."<br>");
But I need to have numbers with decimals... I know it should look something like this:
$row['price'] = intval(($row['price']*100))/100;
But it Does not work.

The simplest way is to store your data in database with type of DECIMAL or FLOAT or DOUBLE. And when you will output data from database it will already be in decimal format. Which type to use is relative. Look threw the web to find optimal solution for your situation.
If you want to use PHP use number_format()
$num = "18";
echo number_format((float)$num, 2, '.', ''); //echo will output 105.00

Its really a lot confusing line
echo number_format($_SESSION['price_summ']."<br>");
I guess you want to convert $_SESSION['price_summ'] to float .
Use floatval() for it.
echo number_format(floatval($_SESSION['price_summ']))."<br>";
Alsi you put the bracket after "<br>" which is wrong. In your second code segment you ate simply doing nothing if you $row['price'] is an integer. If it is an integer, go for this
$row['price'] = intval(($row['price']*100.0))/100;

Related

dechex function outputs wrong result

I have a simple problem but fairly complex and I could use some advice on how to solve it.
I've been trying all solutions of questions asked before but none of them seem to work.
echo dechex(0.258068 * 1000000000000000000); //returns 394d77a8be54020
The correct value however is
394D77A8BE54000
how can I convert it to the correct value, I understand is a big integer and that is a problem, but how can I obtain correct values inside my script ?
You're correct: you're getting a floating point inaccuracy error. Note that 0.258068 * 1000000000000000000 is a float, not an integer.
Here's a solution:
echo 0.258068 * 1000000000000000000;
echo PHP_EOL;
$i = bcmul(0.258068, 1000000000000000000);
echo $i;
echo PHP_EOL;
$h = dechex($i);
echo $h;
Result:
2.58068E+17
258068000000000000
394d77a8be54000
Refs:
Live example
bcmul

How to cut decimal number using PHP

I have a code where I got some numbers like this:
92.682926829268
I'd like to cut them like this:
92.68
This is my code:
<td><?php if (($row['TotalMatch']) > 10){ echo ($row['OK_05'] / $row['TotalMatch']) * 100; } ?></td>
I tried with floor and round but I get that example I showed at the beginning of post ( 92.682926829268 instead of 92.68 )
Thanks for your attention
Regards!
EDIT Could you give me an example with my code? Thanks
Use sprintf() to format the number.
echo sprintf("%.2f", 92.682926829268);
Example:
https://3v4l.org/U87T9
The expression you're trying to format is this:
($row['OK_05'] / $row['TotalMatch']) * 100
So whichever function you decide to use needs to go around that expression.
As to which function to use, you need to select one that returns a string, not a float.
If you use round, and your expression returns a float that rounds to a number with two zeros after the decimal point, the trailing zeros will not be displayed in the result. For example, echo round(92.0006829268, 2) will display 92, not 92.00. So don't use round if you need to be sure that two decimal places are always displayed. round is a math function, not a formatting function.
floor is really not useful at all here, as it returns a number with no decimal places.
A simple way is to use sprintf as shown in some of the other answers.
echo sprintf("%.2f", ($row['OK_05'] / $row['TotalMatch']) * 100);
The first argument to sprintf is "%.2f", which is a format string indicating that the second argument should be displayed as a float with two decimal places. The second argument is your expression.
Using bcdiv as suggested in the other answer will also work, but it works a little differently that sprintf and will produce a slightly different result in some cases.
sprintf will round to the number of decimal places specified, so for example
echo sprintf("%.2f", 926.89 / 10); // outputs 92.69
and bcdiv will truncate instead, so
echo bcdiv(926.89, 10, 2); // outputs 92.68
Whichever one of those works for you, do that.
You can use the round function
$var = 92.682926829268;
$var = round($var, 2)
Or use sprintf (%.2f cuts the number)
$var = sprintf("%.2f", $var);
Try using sprintf like below:
<?php
$mynumber = 98.343434;
echo sprintf('%.2f', $mynumber); // this will output 98.34
You could use bcdiv()
bcdiv($row['OK_05'], ($row['TotalMatch'] * 100), 2);

How to convert a "decimal string" into an integer without the period in PHP

If I have, say, 8.1 saved as a string/plaintext, how can I change that into the integer (that I can do addition with) 81? (I've got to remove the period and change it into an integer. I can't seem to figure it out even though I know it should be simple. Everything I try simply outputs 1.)
You can also try this
$str = '8.1';
$int = filter_var($str, FILTER_SANITIZE_NUMBER_INT);
echo $int; // 81
echo $int+1; // 82
DEMO.
If you're dealing with whole numbers (as you said), you could use the intval function that is built into PHP.
http://php.net/manual/en/function.intval.php
So basically, once you have your string parsed and setup as a whole number you can do something like:
intval("81");
And get back the integer 81.
Example:
$strNum = "81";
$intNum = intval($strNum);
echo $intNum;
// "81"
echo getType($intNum);
// "integer"
Since php does auto-casting, this should work:
<?php
$str="8432.145522";
$val = str_replace('.','', $str);
print $str." : ".$val;
?>
Output:
8432.145522 : 8432145522
Not sure if this will work. But if you always have something.something,(like 1.1 or 4.2), you can multiply by 10 and do intval('string here'). But if you have something.somethingsomething or with more somethings(like 1.42 and 5.234267, etc.), I don't know what to say. Maybe a function to keep multiplying by ten until it's an integer with is_int()?
Sources:
http://php.net/manual/en/function.intval.php
http://php.net/manual/en/function.is-int.php
Convert a string to a double - is this possible?

Php set value as a number

How do I output a value as a number in php? I suspect I have a php value but it is outputting as text and not as a number.
Thanks
Here is the code - Updated for David from question below
<?php
if (preg_match('/\-(\d+)\.asp$/', $pagename1, $a))
{
$pageNumber = $a[1];}
else
{ // failed to match number from URL}
}
?>
If I call it in: This code it does not seem to work.
$maxRows_rs_datareviews = 10;
$pageNum_rs_datareviews = $pagename1; <<<<<------ This is where I want to use it.
if (isset($_GET['pageNum_rs_datareviews'])) {
$pageNum_rs_datareviews = $_GET['pageNum_rs_datareviews'];
}
If I make page name a static number like 3 the code works, if I use $pagename1 it does not, this gives me the idea $pagename1 is not seen as a number?
My stupidity!!!! - I used $pagename1 instead of pageNumber
What kind of number? An integer, decimal, float, something else?
Probably the easiest method is to use printf(), eg
printf('The number %d is an integer', $number);
printf('The number %0.2f has two decimal places', $number);
This might be blindingly obvious but it looks like you want to use
$pageNum_rs_datareviews = $pageNumber;
and not
$pageNum_rs_datareviews = $pagename1;
echo (int)$number; // integer 123
echo (float)$number; // float 123.45
would be the easiest
I prefer to use number_format:
echo number_format(56.30124355436,2).'%'; // 56.30%
echo number_format(56.30124355436,0).'%'; // 56%
$num = 5;
echo $num;
Any output is text, since it's output. It doesn't matter what the type of what you're outputting is, since the human eye will see it as text. It's how you actually treat is in the code is what matters.
Converting (casting) a string to a number is different. You can do stuff like:
$num = (int) $string;
$num = intval($string);
Googling php string to number should give you a beautiful array of choices.
Edit: To scrape a number from something, you can use preg_match('/\d+/', $string, $number). $number will now contain all numbers in $string.

PHP calculation

I'm trying to output a value from xml this is what I do -
<?php
echo $responseTemp->Items->Item->CustomerReviews->AverageRating;
?>
This outputs 4.5, but when I change it to the code below it displays as 8. Why isn't it displaying as 9? Thanks.
<?php
echo $responseTemp->Items->Item->CustomerReviews->AverageRating*2;
?>
Try casting the value to a numerical value first.
$num = (double) $responseTemp->Items->Item->CustomerReviews->AverageRating;
echo $num * 2;
See Type Juggling and String Conversion to Numbers on the PHP website for more information.
If you are looking for a decimal value without doing typecasting, you have to multiply by a number with a decimal. Otherwise it will return a regular integer like the number you gave it.
Try multiplying by 2.0
echo $responseTemp->Items->Item->CustomerReviews->AverageRating*2.0;

Categories