Skip to content

Instantly share code, notes, and snippets.

@ppazos
Created April 7, 2021 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppazos/eb784f1de263f8fc8eb18528ac6acb49 to your computer and use it in GitHub Desktop.
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"
<?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