Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

sprintf add two xeroes for a float number

$num = 42.0815;

function printFloatWithLeadingZeros($num, $precision = 2, $leadingZeros = 0){
    $decimalSeperator = ".";
    $adjustedLeadingZeros = $leadingZeros + mb_strlen($decimalSeperator) + $precision;
    $pattern = "%0{$adjustedLeadingZeros}{$decimalSeperator}{$precision}f";
    return sprintf($pattern,$num);
}

for($i = 0; $i <= 6; $i++){
    echo "$i max. leading zeros on $num = ".printFloatWithLeadingZeros($num,2,$i)."
";
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #sprintf #add #xeroes #float #number
ADD COMMENT
Topic
Name
1+9 =