Originally Posted by: brotjob Originally Posted by: zapzapouch It should be possible to specify the unit as "m", and it automatically displays "10 nm" or "10 km" when the number changes, without manually typing in "km".
Can you specify such an algorithm?
Yes, here are two examples, if Andrei would like a template:
In Javascript
http://www.i2cchip.com/miscstuff/functionLib.js(function is Real2Eng)
In Pascal:
http://www.i2cchip.com/miscstuff/engfmt.zipIt is inside spstring.pas. The helpfile explains in detail what the function Float2EngStr does:
function Float2EngStr(R:extended;MantissaDigits:word;FormatStr:string; Suffix:string):string
turns a real no. into a Engineering formatted string of PRECISION digits accuracy,
Engineering Format shifts exponent to powers of 3 (familiar to HP calculator users)
Will also suffix appropriate letter multipliers instead of E+XX eg k,M etc
To use defaults try Float2EngStr(1.0123456E4,0,'','' ) -> ' 10.12E+03'
MantissaDigits: number of digits (not including decimal) min is 3; 4 recomended
(default=4)
FormatStr: This set of chars sets how sign and exponents appear.
(default=' E-2' )
Format Str[1] sets how the leading char or sign appears
' ' leading <space> for +ve , - for -ve
'|' no leading char for +ve, - for -ve
'+' always has leading + or -;
'-' no leading char for +ve,reduces precision by 1 for -ve to accomodate -
only if precision>=4; recommended for >=5. Keeps length constant for +ve & -ve #'s
FormatStr[2] sets exponent style and case. if lower case will use lower case 'e'
'E','e' : force exponential form
'F','f' : use prefix chars where possible (ie for E-15 to E+14 )
FormatStr[3] sets how exponent sign appears
'+' : always have + or - (string will be constant length)
'|' : no + for +ve, - for -ve (string will change length)
'-' : use + for +ve unless exponent>digits, then drop + to keep length constant
' ' : <space> is NOT A VALID OPTION. Pascal read routines require EXXX not E XXX
FormatStr[4] number of digits in ExponentDigits (range '1' to '4' )
If exponent has more digits than ExponentDigits (very large or very close to zero)
then length will be greater. See ClipFloat()
If either FormatStr or MantissaDigits are invalid, sets to default.
eg
Float2EngStr(1.978269E+07,5,' F-2','Hz' ) = '19.783 MHz'
Float2EngStr(1.978269E+07,5,' E-2','' ) = '19.783E+06'
Float2EngStr(1.978269E-02,5,'-f-2','' ) = '-19.78e-03'
Float2EngStr(1.978269E+103,5,'-f-2','' ) = '19.783e102'
Float2EngStr(-1.978269E+103,5,'-F-2','' ) = '-19.78E102'
Edited by user 06 December 2014 10:50:52(UTC)
| Reason: Not specified