Home > database >  converting decimal number into long integer
converting decimal number into long integer

Time:05-25

I have the number 43.95
I want to convert it to the become 4395. How do I simply do this is 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);

CodePudding user response:

<?php
$dec = 43.95;
echo $int = (int)str_replace('.', '', (string)$dec);//4395

CodePudding user response:

If numbers after decimal will always be 2 then multiply by 100

<?php
  $price = 43.95;
  echo $price*100;

https://3v4l.org/CN9T4

Otherwise, you can use the @Diego De Vita code given in the comments under your question.

  •  Tags:  
  • php
  • Related