Skip to content

Instantly share code, notes, and snippets.

View davidmweber's full-sized avatar

David Weber davidmweber

  • Monadic Consulting
  • Cape Town
View GitHub Profile
@davidmweber
davidmweber / unit.ts
Last active April 14, 2025 09:55
Printing engineering units in typescrit
const posPrefix = ["", "k", "M", "G", "T", "P"]
const negPrefix = ["", "m", "μ", "n", "p", "a"]
/**
* Convert a number to a metric string in engineering/SI notation. For example,
* toMetric(1.2345678e8, "Wh", 4) returns "123.5MWh".
* @param value The number to convert to a string.
* @param unit A string representing the unit to use e.g. kW, C, V expected
* @param The total number of digits to return
*/