converting decimal number into long integer - php

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

Related

PHP Numeric calculation e-5 e-6

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;

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

convert scientific notation to decimal with php

math with bitcoin is giving me problems
$value = bcmul((float)$TotalMoney, $p,8);
$value = bcdiv((float)$Value, 100,8);
returns 8.431e-05 as one of the values in the script
i've tried
$newNum = (float)$value;
$newNum = number_format((float)$value, 8);
$newNum = sprintf('%.8f',$value);
function scientific_notation($in_float_value, $in_decimal_place_count = -1)
{
// Get the exponent
$abs_float_value = abs($in_float_value);
$exponent = floor($abs_float_value == 0 ? 0 : log10($abs_float_value));
// Scale to get the mantissa
$in_float_value *= pow(10, -$exponent);
// Create the format string based
// on the requested number of decimal places.
$format = ($in_decimal_place_count >= 0) ? "." . $in_decimal_place_count : "";
//echo("Format0: $format");
// Format the exponent part using zero padding.
$formatted_exponent = "+" . sprintf("%02d", $exponent);
if($exponent < 0.0)
{
$formatted_exponent = "-" . sprintf("%02d", -$exponent);
}
$format = "%" . $format . "fe%s";
//echo("Format1: $format");
// Return the final value combining mantissa and exponent
return sprintf($format, $in_float_value, $exponent);
}
$newNum = scientific_notation($value,8);
Tried it in phpfiddle and it works. maybe the problem is storing it in a db. It's stores as 8.431e-05 in the database
what am I doing wrong?
Use the exemple below to convert Scientific Notation to float/decimal on PHP:
echo sprintf('%f', floatval('-1.0E-5'));//default 6 decimal places
echo sprintf('%.8f', floatval('-1.0E-5'));//force 8 decimal places
echo rtrim(sprintf('%f',floatval(-1.0E-5)),'0');//remove trailing zeros
When working with Bitcoin balances it is recommended to store amounts in a database in satoshis as an integer and then you can convert it back to 8 decimals when displaying it on the screen to users.
$amount = 0.0132;
$convert = $amount * 100000000;
// store in DB as the converted amount 1320000 as an integer
// when grabbing from DB convert it back
$databaseValue = 1320000;
$convertBack = $databaseValue / 100000000;
$display = number_format($convertBack, 8);
echo $display;

Adding numbers doesn't give expected results

I have 3 numbers returned from a database that I need to add together. For a total of 1,740.01, I get like 10.00. These are floating point numbers. Here are the actual numbers I'm having a problem with.
> 1,729.13
> 10.88
> 0.00
How do I get the correct total of these numbers?
EDIT
$cash = preg_replace('/\$/', '', $cash);
$card = preg_replace('/\$/', '', $card);
$check = preg_replace('/\$/', '', $check);
$cash = number_format(array_sum($cash), 2);
$card = number_format(array_sum($card), 2);
$check = number_format(array_sum($check), 2);
$total = number_format( $cash + $card + $check, 2 );
Don't add values that have been formatted using number_format(), otherwise they may well contain , as a thousand's separator... they're treated as strings rather than floats.
Assuming that $cash, $card and $check are arrays:
$cash = array_sum($cash);
$card = array_sum($card);
$check = array_sum($check);
$total = number_format( $cash + $card + $check, 2 );
$cash = number_format( $cash, 2 );
$card = number_format( $card, 2 );
$check = number_format( $check, 2 );
Do any formatting after the math
EDIT
Use str_replace() rather than preg_replace() to strip out any $, then it's simple addition rather than needing to do the array_sum(), though I'm not sure where any $ is likely to come from if the values are DECIMAL 10,7 from the database
$cash = (float) str_replace( array('$',','), '', $cash );
$card = (float) str_replace( array('$',','), '', $card );
$check = (float) str_replace( array('$',','), '', $check );
$total = number_format( $cash + $card + $check, 2 );
$cash = number_format( $cash, 2 );
$card = number_format( $card, 2 );
$check = number_format( $check, 2 );
(assumes that , is your thousands separator in the string values for $cash, $card and $check when doing the str_replace()
EDIT 2
To convert the $cash array to a sum of its values, having cleaned up any $ or , in the strings:
$cash = array ( 0 => '$1729.13', 1 => '0.00', 2 => '$1,234.56' );
function cleanArrayValues($value) {
return (float) str_replace( array('$',','), '', $value );
}
$cash = array_sum(array_map('cleanArrayValues',$cash));
I've added an extra value into the $cash array just to demonstrate.
You'll need to filter $card and $checks in the same way, using the cleanArrayValues() callback function (if they're all arrays)

Split a number by decimal point in php

How do I split a number by the decimal point in php?
I've got $num = 15/4; which turns $num into 3.75. I would like to split out the 3 and the 75 parts, so $int = 3 and $dec = 75. My non-working code is:
$num = 15/4; // or $num = 3.75;
list($int, $dec) = split('.', $num);
but that results in empty $int and $dec.
Thanks in advance.
If you explode the decimal representation of the number, you lose precision. If you don't mind, so be it (that's ok for textual representation). Take the locale into account! We Belgians use a comma (at least the non-programming ones :).
If you do mind (for computations e.g.), you can use the floor function:
$num = 15/4
$intpart = floor( $num ) // results in 3
$fraction = $num - $intpart // results in 0.75
Note: this is for positive numbers. For negative numbers you can invert the sign, use the positive approach, and reinvert the sign of the int part.
$num = 15/4; // or $num = 3.75;
list($int, $dec) = explode('.', $num);
Try explode
list($int,$dec)=explode('.', $num);
as you don't really need to use a regex based split. Split wasn't working for you as a '.' character would need escaping to provide a literal match.
$int = $num > 0 ? floor($num) : ceil($num);
$dec = $num - $int;
If you want $dec to be positive when $num is negative (like the other answers) you could do:
$dec = abs($num - $int);
$num = 3.75;
$fraction = $num - (int) $num;
In case when you don't want to lose precision, you can use these:
$number = 10.10;
$number = number_format($number, 2, ".", ",");
sscanf($number, '%d.%d', $whole, $fraction);
// you will get $whole = 10, $fraction = 10
This works for positive AND negative numbers:
$num = 5.7;
$whole = (int) $num; // 5
$frac = $num - (int) $num; // .7
$num = 15/4;
substr(strrchr($num, "."), 1)

Categories