I've built an algorithm to convert meters into ... other things.
See log.
Here's an input:
Here's the output:
Generator notes:
Click me to show info in the console
Story
Convert any distance in meters into a relevant unit – from nanometers to astronomical units.
Avoid over-precision ie don't include meters in 100s of km
Example output
1 m is 1 m
0.009 m is 9 mm
.01 m is 1 cm
999 m is just under 10 km
Sources for Metric Units
Configuration for Metric Units
metricUnits = new Array( {name:"nm", dist:1e-9, dp:"3"}, {name:"μm", dist:1e-6, dp:"3"}, {name:"mm", dist:0.001, dp:"3"}, {name:"cm", dist:0.01, dp:"any"}, {name:"m", dist:1, dp:"any"}, {name:"km", dist:1000, dp:"2"}, {name:"km", dist:1000000, dp:"-2", adjust:"1000"}, {name:"au", dist:1.496e+11, dp:"2"}, {name:"ly", dist:9.461e+15, dp:"2"}, {name:"mPc", dist:3.086e+22, dp:"2"} );
Fixes
Algorithm: better handling of negative and zero values
Testing: Added two tests to verify changes
Testing: Bulk data generation avoids numeric problems (i.e. step of 0.001 no longer occasionally produces x.xx0000000000000001)
Testing: Deeper info on metric units: sources and configuration used
Known Issues
- 001: 0.011 m is 1.0999999999999999 cm
Tests
Show / manipulate object in console
This will show you the object-under-test in the console:
console.log(workroomprds.converter.NewConverter(workroomprds.converter.metricUnits))
. Open the console to see it and to work with it directly.