I need a function in PHP to move the decimal to the beginning of the number if one exists otherwise if there is no decimal add 0. to the beginning.
I have:
function toDecimal($input){
return (stripos($input, ".")!==false)? $input: "0." . $input;
}
which was provided in a previous question of mine (thanks @shiplu.mokadd.im) but I need to extend it to also move the decimal to the beginning like:
Input Output
0.1234 0.1234
1.2345 0.12345
1234 0.1234
0.001234 0.001234
so basically the outputted number can never be larger than 1.
Thanks!
A little recursive magic should do the trick:
EDIT:
Here’s a loop version (recursion was the first thing that came to mind):