Rounding values to a defined number of significant digits
chozo
Function SuperRound(number,digits) // round values to selectable number of digits
Variable number, digits
String StrNumber
Variable Multiplier, i // multiplier decides how much the decimal spot is "shifted" for rounding
if (number < 1) // separate numbers whether they are < or > 1
sprintf StrNumber, "%.10f", number // express the number as text to work with it
for (i = 2; i < Strlen(StrNumber); i += 1)
if(StringMatch(StrNumber[i],"0") == 0) // find the first non-zero number
Multiplier = 10^(2 - i - digits)
break
endif
endfor
else
sprintf StrNumber, "%d", number // express the number as text to count the digits
Multiplier = 10^(strlen(StrNumber) - digits)
endif
if (Multiplier < 1) // countermeasure for rounding errors when dividing by small numbers
Multiplier = 1/Multiplier
number = round(number*Multiplier)/Multiplier
else
number = round(number/Multiplier)*Multiplier
endif
return number
End
Variable number, digits
String StrNumber
Variable Multiplier, i // multiplier decides how much the decimal spot is "shifted" for rounding
if (number < 1) // separate numbers whether they are < or > 1
sprintf StrNumber, "%.10f", number // express the number as text to work with it
for (i = 2; i < Strlen(StrNumber); i += 1)
if(StringMatch(StrNumber[i],"0") == 0) // find the first non-zero number
Multiplier = 10^(2 - i - digits)
break
endif
endfor
else
sprintf StrNumber, "%d", number // express the number as text to count the digits
Multiplier = 10^(strlen(StrNumber) - digits)
endif
if (Multiplier < 1) // countermeasure for rounding errors when dividing by small numbers
Multiplier = 1/Multiplier
number = round(number*Multiplier)/Multiplier
else
number = round(number/Multiplier)*Multiplier
endif
return number
End
Forum
Support
Gallery
Igor Pro 9
Learn More
Igor XOP Toolkit
Learn More
Igor NIDAQ Tools MX
Learn More
variable value, numSigDigits
string str
sprintf str, "%.*g\r", numSigDigits, value
return str2num(str)
End
print DoStuff(0.003474, 2) 0.0035
January 13, 2016 at 02:52 am - Permalink
January 13, 2016 at 10:55 pm - Permalink