-1

I have this number

$sku = '2200081005966';

and I want to convert the number like this without rounding the number

$new_sku = '5.96' 

I already try this number_format(substr($sku, 7, 12), 0, '', '.') but the output that I get 5.97.

Any ideas how can I make this work?

Thank you.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
N.I.A
  • 75
  • 1
  • 7
  • This looks like a PHP question, not a Javascript question? – Snow Nov 22 '19 at 07:31
  • Yup, php question..my mistake – N.I.A Nov 22 '19 at 07:33
  • 1
    I'm sorry, maybe I don't understand it correctly but what are the general rules of this conversion? Get substring from 7th character with decimal dot in the middle and cut after two decimal digits? – kaczmen Nov 22 '19 at 07:48
  • Just perform the truncation BEFORE formatting. `var_export(number_format(substr($sku, 7, 11), 0, '', '.'));` ...or I don't understand your requirements. – mickmackusa Nov 22 '19 at 07:56

2 Answers2

1

Add floor() around your number_format:

$sku = '2200081005966';
echo floor(number_format(substr($sku, 7, 12), 0, '', '.')*100)/100;

Outputs:

5.96

Note: It would works well in case of positive numbers, your substr always take a positive number, that's why it would be enough.

Aksen P
  • 4,564
  • 3
  • 14
  • 27
1
<?php
$sku = '2200081005966';
$foo=substr($sku, 9, 12);
echo substr($foo, 0,2).".".substr($foo, 3,3);
?>

first you get the last 4 charakters of the string, then you split that part and put a dot between.

Mario
  • 275
  • 1
  • 10