PHP Numeric calculation e-5 e-6 - php

When I multiply two numbers with php I get results like 3.12E-5 0.15E-6
How can I make these numbers like 0.0000010212?
Example
$amount = "100.00"; <<--- Variable
$bitcoin = "0.00000312"; <<--- Variable
$calculation = $amount * $bitcoin;
echo $calculation;
// Result : 3.12E-5

I'm a fan of the BC Math functions for things like this. In this example I've used bcmul() to multiply the values and provided 8 as the scale to support a single satoshi:
$amount = "100.00";
$bitcoin = "0.00000312";
$calculation = bcmul($amount, $bitcoin, 8);
echo $calculation; //0.00031200

$amount = floatval("100.00");
$bitcoin = floatval("0.00000312");
$calculation = $amount * $bitcoin;
$calculation = number_format($calculation,6,'.',',');
echo $calculation;

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

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

php sum with add symbol as variable

I have this variable
$option_value['price'] = 2
$option_value['price_prefix'] = +
$this->data['price2'] = 3
I have tried to make sum from it like this
$price = $option_value['price'].''.$option_value['price_prefix'].''.$this->data['price2'];
echo $price
but the result is 2. What I want is 5.
Prease help
Under the assumption that the prefix is always either + or -, something like this should do the trick (I've changed the variable names for the sake of readability):
$price = 2;
$prefix = "+";
$price2 = 3;
$total = $price + ($prefix.$price2);
This concatenates the prefix and the second price to "+3" which will then be cast implicitly to an integer for the addition with the first price. The parentheses make sure that the concatenation is done before the addition. Otherwise the addition would precede and that would lead to concatenation rather than addition.
You can do this like this:
<?php
$option_value['price'] = 2;
$option_value['price_prefix'] = '+';
$option_value['price2'] = 3;
$price = $option_value['price']+$option_value['price_prefix']+$option_value['price2'];
echo $price;
?>

PHP Float Price Wrong Value?

$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;
?>

PHP: 3 int IDs within 1 int variable

I'm having to pass 3 variables (int) within a single numeric string called $id. To do this I'm creating $id using padding which I can then explode to get the variables. It has to be numeric otherwise I'd use underscores between the variables. I'm using eleven zeros as padding as I know the variables won't have that many zeros. So currently if I have:
$int_one = 1;
$int_two = 2;
$int_three = 3;
That would be:
$id = "1000000000002000000000003";
To create the new Id I use:
$id = $int_one . "00000000000" . $int_two . "00000000000" . $int_three;
And to separate the Id I use:
$int_one = 0;
$int_two = 0;
$int_three = 0;
if (strpos($id,"00000000000") !== false) {
$id = strrev($id); // Reversed so 0's in int's don't get counted
$id = explode("00000000000", $id);
// Set numbers back the right way
$int_one = strrev($id[2]);
$int_two = strrev($id[1]);
$int_three = strrev($id[0]);
}
This runs into problems when an individual variables is 0. Is there a way to overcome this or does it need a major rethink?
EDIT: $id is supposed to be a numeric string not int
Needs to handle int variables between 0 - 2147483647
You can just use some string magic to assure that no number has more than one zero in a row, and delimit the values using '00'. This generates a numeric string that can be uniquely decoded no matter the size or composition of the ints.
$a = 100;
$b = 0;
$c = 120;
// Encode;
$id = str_replace('0', '01', $a).'00'
.str_replace('0', '01', $b).'00'
.str_replace('0', '01', $c);
// $id = "101010001001201"
// Decode;
$tmp = split('00', $id);
$a2 = intval(str_replace('01', '0', $tmp[0]));
$b2 = intval(str_replace('01', '0', $tmp[1]));
$c2 = intval(str_replace('01', '0', $tmp[2]));
// $a2 = 100, $b2 = 0, $c2 = 120
Is there a way to overcome this or does it need a major rethink?
Yes, you'll need to rethink that. Why do you need to do it that way? Simply create a function with three parameters and pass the three ints in:
function foo($int1, $int2, $int3) {
}
Your example uses strings, not ints by the way, so you aren't even following your own requirements.
You could try this method:
$int_one = 1;
$int_two = 2;
$int_three = 3;
$id = $int_one * 1000000000000 + $int_two * 1000000 + $int_three;
// This will create a value of 1000002000003
To reverse the process:
// Get the modulo of $id / 1000000 --> 3
$int_three = $id % 1000000;
// Recalculate the base id - if you would like to retain the original id, first duplicate variable
// This would make $id = 1000002;
$id = ($id - $int_three) / 1000000;
// Again, get modulo --> 2
$int_two = $id % 1000000;
// Recalculate base id
$id = ($id - $int_two) / 1000000;
// Your first integer is the result of this division.
$int_one = $id;

Categories