PHP Float Price Wrong Value? - php

$price = $product->fiyat1 / $currency_values[0];
im trying to do this values are
$product->fiyat1 = 50,75
$currency_values[0] = 1.0000
it is turning this value 50.0000 but i need this 50.7500

<?php
$product_fiyat1 = '50,75';
$currency_values[0] = 1.0000;
$number1 = floatval(str_replace(',', '.', str_replace('.', '', $product_fiyat1)));
$number2 = floatval($currency_values[0]);
$price = $number1 / $number2;
echo $price;
?>

From Floating point numbers:
Floating point numbers (also known as "floats", "doubles", or "real
numbers") can be specified using any of the following syntaxes:
<?php
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
?>

Related

converting decimal number into long integer

I have the number 43.95
I want to convert it to the become 4395.
How do I simply do this in 1 line?
I currently have it like this:
$priceDollars = intval($priceSum);
$priceCents = $priceSum - $priceDollars;
$priceCents = round($priceCents, 2);
$priceCents = substr((string)$priceCents, 2);
print_r("$priceDollars and $priceCents");
$priceDollars=(string)$priceDollars;
$priceCents=(string)$priceCents;
$price = $priceDollars.$priceCents;
$price = (int)$price;
print_r($price);
<?php
$dec = 43.95;
echo $int = (int)str_replace('.', '', (string)$dec);//4395
a) If numbers after decimal will always be 2 then multiply by 100
<?php
$price = 43.95;
echo $price*100;
https://3v4l.org/CN9T4
b) Otherwise, you can use the #Diego De Vita code given in the comments under your question.
intval( str_replace(".", "", $pricesum) );
https://3v4l.org/dUXm2

How to round numbers?

7000 / 253000 = 0.027667984189723
$number1 = "7000";
$number2 = "253000";
$total = $number1 / $number2;
// return 0.027667984189723
This result how to round 2766 ?
Try this
echo round($total,5);
$number1 / $number2 * 10000 % 1
I'm not sure how you came up with 2766 as a result of your decimal. But I'm up for that challenge. Firstly, to get the correct decimal placing do:
<?php echo round($total, 5); ?>
However, if you're like me, I'm going to figure out that challenge and have some fun with it:
$num1 = round($total, 5);
$num2 = strval($num1);
$num3 = str_split($num2);
$arr1 = array();
foreach ($num3 as $num) {
if(is_string($num)) {
array_push($arr1, $num);
}
}
$arr2 = array_slice($arr1, 3);
foreach($arr2 as $out) {
echo $out;
}
The expected output is 2767, since I did it with the correct round function.
I hope it works for you i tried with different values its working good when value start with 0.
$after_point = ltrim($total,"0."); //27667984189723
echo substr($v,0,4); //2766
if you give 52.548457 then it return 52.5
You must chack that number start with 0.

Can someone demonstrate a variable approximation to echo final variable results more concisely

Can anyone provide an alternative answer to my variable summations without it being a floating point decimal result with the possibility of displaying the final result as in an interger value. Whole numbers to be exact.
With every variable passing results in decimal notation, the final value is larger than the actual pen to paper I get using calculator.
This yeld s echo $terresulting in float value 43402777.777778. It should be more in the lines of 42,849,000 instead of the former, the latter value is my pen and paper. The problem is outputting final value to be non scientific notation. I need in straight interger value.
$tan = 100000;
$g = 30;
$epp = 12;
$days = 365;
$bpe = 4;
$pp = 300;
$apg = $tan / $g;
$epg = $apg / $bpe;
$ppg = $epg / $epp;
$ypr = $ppg * $pp;
$tep = $ppg * $g;
$ter = $ypr * $tep;
echo $ter;
As is typical in computer science, floating-point arithmetic comes with its own set of problems.
Libraries are generally available for more precision. PHP offers the BC Math functions.
Your example can be altered to use this...
$tan = 100000;
$g = 30;
$epp = 12;
$days = 365;
$bpe = 4;
$pp = 300;
$apg = bcdiv($tan, $g);
$epg = bcdiv($apg, $bpe);
$ppg = bcdiv($epg, $epp);
$ypr = bcmul($ppg, $pp);
$tep = bcmul($ppg, $g);
$ter = bcmul($ypr, $tep);
echo '$ter: ', $ter, PHP_EOL; // 42849000
echo 'Formatted: ', number_format($ter), PHP_EOL; // 42,849,000
Demo ~ https://3v4l.org/vlRaS

Round small numbers

I would like to know how to achieve this:
round(0.38)
And receive this:
0.40
How can I do this? I tried with round(0.38, 2) but it does not work.
To get 0.4, round to 1 decimal place using round() where the second parameter is the decimal place.
$rounded_number = round(0.38, 1);
To get 0.40, use number_format() to get 2 decimal places.
$formatted_number = number_format( $rounded_number , 2);
Multiply by ten, round, then divide by ten:
php > $a = 0.38;
php > $b = $a * 10;
php > $c = round($b);
php > echo $c/10;
0.4
If you want the two decimal places:
$num = 0.38;
$num = round($num * 10)/10;
$num = number_format($num, 2);
echo $num;
It is probably far simpler than you think it is:
$num = round(0.38 * 10)/10;
echo $num;
Then, you could just replace 0.38 with another number or a variable.
$a = 0.38;
echo sprintf("%.2F", round($a,PHP_ROUND_HALF_UP));

How can I make calculation pieces in PHP?

How can I make calculation pieces in PHP?
This is my calculation
605,00 / 5% = 30.25.
How can I calculate this in PHP?
$a = 605.00;
$b = 5 (percentage)
How I have tried, but this did not work
$total = ($a / 0.5);
You could do:
$number = 605;
$percentage = 5;
$total = $number * ($percentage / 100);
Actually if you divide 5 by 100, it will equal 0.05 so try $b = .05;.
In basic math, the first decimal is calculated in 10ths, then 100ths, then 1000ths
Just do $total = ($a / $b);
Tested. Total = 12,100 yet will echo 12100
<?php
$a = 605.00;
$b = 0.05;
$total = ($a / $b);
echo $total; // will echo 12100
?>

Categories