daliahdaud
daliahdaud

Reputation: 13

Subtract string and convert to decimal without rounding

I have a number declaration

$barcode_scan = '220008100383005193';

and I want the output like this 0.519.

I already try this code

number_format(substr($barcode_scan, -6), 0, '', '.');

but the output is 5.193. Any help? Thanks

Upvotes: 0

Views: 60

Answers (2)

D A P Peiris
D A P Peiris

Reputation: 1

Try with following code

number_format(substr($barcode_scan, -6)/10000, 3)

OR

number_format(substr($barcode_scan, -6)/10000, 3, '.', ',')

That is conflict of decimal point symbol and thousand separator symbol

Upvotes: 0

Aksen P
Aksen P

Reputation: 4599

Something similar was here.

In your case code should be next:

$barcode_scan = '220008100383005193';
echo floor(number_format(substr($barcode_scan, -6), 0, '', '.')*100)/1000;

Outputs:

0.519

Upvotes: 1

Related Questions