I have an orderId which obviously is variable.
I need to compute another 9 digit number based on that.
Example:
$orderId = 100;
$path should be = 0000000100;
----
$orderId = 2350;
$path should be = 0000002000;
---
$orderId = 7500;
$path should be = 0000007000;
Thanks a lot in advance!
Amit Rajput
2,0591 gold badge13 silver badges27 bronze badges
-
ok. And what will be 9 digit number for 7999 according to you?Amit Rajput– Amit Rajput2016年07月19日 06:56:26 +00:00Commented Jul 19, 2016 at 6:56
-
Have you tried anything so far? Leaving that aside, I don't see a clear rule on how to build that. Do you round down if it's x.5? Do you round up if it's bigger than x.5? I can only assume you round down at this point.Andrei– Andrei2016年07月19日 06:57:27 +00:00Commented Jul 19, 2016 at 6:57
-
First off, your examples are 10 instead of 9 digits? So, the general logic is: keep the first digit of orderId, append as many zeros at the end to keep the number of digits from the original and then fill up the front with zeros to reach 9 digits?AsheraH– AsheraH2016年07月19日 06:59:04 +00:00Commented Jul 19, 2016 at 6:59
1 Answer 1
Try like this
<?php
$test = strval(7001);
echo substr_replace(str_pad("",9,"0"),$test[0],(strlen($test)-1)*(-1),0);
?>
Check here : https://eval.in/607614
Another way is
<?php
$test = 658;
$length = 10-strlen($test);
$str_pad = "0000000000";
$str_pad[$length] = strval($test)[0];
echo $final = $str_pad
?>
Check here : https://eval.in/607610
answered Jul 19, 2016 at 7:06
Niklesh Raut
34.9k17 gold badges82 silver badges112 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Niklesh Raut
@DhavalDave : Changed
10 to 9 logic is samelang-php