Created
April 7, 2021 04:41
-
-
Save ppazos/eb784f1de263f8fc8eb18528ac6acb49 to your computer and use it in GitHub Desktop.
Transform a number representing the precision of another number, into a string containing the step value for HTML input number atttribute "step"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// transforms p=3 into s=0.001 | |
function precision_to_step($precision = 0) | |
{ | |
if ($precision <= 0) return '1'; | |
$step = str_pad('1', $precision, "0", STR_PAD_LEFT); | |
$step = substr_replace($step, '0.', 0, 0); | |
return $step; | |
} | |
foreach (range(-2, 5) as $precision) | |
{ | |
echo "p=$precision, s=". precision_to_step($precision). PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment