I have the weight grabbed from an xml as 0.800, I want to convert it to 000008. This Eg. 003456 is 345.6 kilograms shows how it is to be represented for heavier weights, could someone give me a heads up as to where I could start with this one?
thanks!
EDIT: I have the solution but does anyone know why it cuts off the last digit? See below:
XML:
<GROSSWEIGHT>345.800</GROSSWEIGHT>
PHP:
$LINE_WEIGHT = ($shipment->COLLO->GROSSWEIGHT*10); // 6 CHARS
sprintf("%06s", $LINE_WEIGHT)
// output 003450
EDIT: If i follow this, it works:
echo str_pad(0.800*10, 6, "0", STR_PAD_LEFT);
// output 000008
AS SOON as I include xml data:
echo $shipment->COLLO->GROSSWEIGHT; // 345.800
echo str_pad($shipment->COLLO->GROSSWEIGHT*10, 6, "0", STR_PAD_LEFT);
// output 003450
Please see pic below:
You'd first need to multiply by 10, then have a look at sprintf
, especially the padding option.
use *10
and then "pad-left":
echo str_pad(0.800*10, 6, "0", STR_PAD_LEFT); //000008
echo str_pad(345.6*10, 6, "0", STR_PAD_LEFT); //003456
I'll answer my own question to how I decided to go ahead coding. I used the sprintf solution suggested by simon as it seems a lot cleaner.
XML:
GROSSWEIGHT>0.800</GROSSWEIGHT>
PHP:
$LINE_WEIGHT = (utf8_decode($shipment->COLLO->GROSSWEIGHT)*10); // 6 CHARS
sprintf("%06d", $LINE_WEIGHT);
Thanks everyone!