how about toEngineering and toHexExponential formats for Number

Roger Andrews roger.andrews at mail104.co.uk
Sat Mar 17 04:36:57 PDT 2012


How about a couple of new formats for Number ...

Number.prototype.toEngineering
This is similar to the normalised 'toExponential', but the exponent is a 
multiple of 3 and there are 1, 2, or 3 digits before the decimal point.  The 
format makes it visually easy to relate a string representation of a number 
to SI scaling prefixes ("mega", "giga", "milli", etc).

Many newer pocket calculators provide this format and it seems to be gaining 
ground in formatters generally.  It is useful in engineering, say if one is 
working with microwatts, megaohms, ...

Number.prototype.toHexExponential
This is similar to 'toExponential' but is in the normalised hexadecimal form 
instead of decimal.
E.g.  1.4A2p+4
The letter 'p' corresponds to decimal's 'e', and means "times 2 to the power 
of".
The *binary* exponent is represented in signed *decimal*.
The hex representation should probably be in lowercase to match that of 
'toString(16)'.

GNU's printf provides this format with its %a and %A conversions.  The 
format is useful for transferring arbitrary binary floating-point values 
exactly, whereas decimal formats round them. 



More information about the es-discuss mailing list