Published
Edited
Oct 10, 2020
Insert cell
Insert cell
QuantityStrings = ({
Resistance: { name: 'Resistance', unit: 'Ω' },
Current: { name: 'Current', unit: 'A' },
Length: { name: 'Length', unit: 'm' },
Area: { name: 'Area', unit: 'm²' },
})
Insert cell
format = (value, sdigits=3, unit='', showSpace=true) => {
let rc = d3.format(`.${sdigits}s`)(value)
if(showSpace){
rc = insertSpace(rc)
}
return `${rc}${unit}`
}
Insert cell
format2 = (value, sdigits=3, unit='', showSpace=true, siPrefix='') => {
let rc = siPrefix ? d3.formatPrefix(`.${sdigits}`, siPrefix)(value) : d3.format(`.${sdigits}s`)(value)
if(showSpace){
rc = insertSpace(rc)
}
return `${rc}${unit}`
}
Insert cell
format(0.00345, 3, QuantityStrings.Resistance.unit, true)
Insert cell
format(1.10345, 3, QuantityStrings.Resistance.unit, true)
Insert cell
format2(0.00345, 3, QuantityStrings.Resistance.unit, false, 10e-3)
Insert cell
format2(1.10345, 3, QuantityStrings.Resistance.unit, false, 10e-3)
Insert cell
format2(1.10345, 3, QuantityStrings.Current.unit, true, 10e-3)
Insert cell
format2(0.00345, 3, QuantityStrings.Resistance.unit, true, 10e-3)
Insert cell
format(0.00345, 3, QuantityStrings.Resistance.unit, true)
Insert cell
format(0.00345, 3, QuantityStrings.Current.unit, true)
Insert cell
format(0.0000345, 3, QuantityStrings.Resistance.unit, false)
Insert cell
ScaledQuantities =({
MilliOhm: { base: QuantityStrings.Resistance, ratio: 1000, siPrefix: 'm'},
MilliAmp: { base: QuantityStrings.Current, ratio: 1000, siPrefix: 'm'},
MicroOhm: { base: QuantityStrings.Resistance, ratio: 10e5, siPrefix: 'μ'},
Centimeter: { base: QuantityStrings.Length, ratio: 100, siPrefix: 'c' },
CentimeterSquared: { base: QuantityStrings.Area, ratio: 10000, siPrefix: 'cm²' }
})
Insert cell
getScaled = (value, scaledQuantity) => value * scaledQuantity.ratio
Insert cell
putScaled = (value, scaledQuantity) => value / scaledQuantity.ratio
Insert cell
formatScaled = (value, scaledQuantity, sdigits=3, showUnit=true, showSpace=true, trim=false) => {
let rc = scaledQuantity ? `${(value * scaledQuantity.ratio).toFixed(sdigits)}${showUnit ? scaledQuantity.siPrefix : ''}` : format(value, sdigits, '', false);
if(trim){
rc = trimTrailingZeros(rc)
}
if(showSpace){
rc = insertSpace(rc)
}
const unit = scaledQuantity && showUnit ? scaledQuantity.base.unit : '';
return `${rc}${unit}`
}
Insert cell
formatValue = (value, unit='', sdigits=3, toFixed=false, showSpace=false, trim=false) => {
//let rc = d3.format(`.${sdigits}${trim ? '~' : ''}s`)(value)
let rc = toFixed
? `${value.toFixed(sdigits)}`
: d3.format(`.${sdigits}${trim ? '~' : ''}s`)(value);
if(trim){
rc = trimTrailingZeros(rc)
}
if(unit && showSpace){
rc = insertSpace(rc)
}
return `${rc}${unit}`
}
Insert cell
getScaled(0.02, ScaledQuantities.CentimeterSquared)
Insert cell
formatValue(234.460, '', 3, true, true, true)
Insert cell
trimTrailingZeros('234.460')
Insert cell
formatValue(200, "", 3, true, true, false)
Insert cell
formatValue(200, "", 3, true, true, true)
Insert cell
formatValue(2.00000, "mΩ", 3)
Insert cell
formatValue(2.00000, 'mΩ', 3, false, true)
Insert cell
formatValue(2.00000, 'm²', 3, false, true)
Insert cell
formatValue(2.00400, 'mΩ', 4, false, true)
Insert cell
formatValue(2.00400, 'mΩ', 3, false, true)
Insert cell
formatValue(2.00400, 'mΩ', 3, true, true)
Insert cell
formatValue(2e-2, 'mΩ', 3, true, true)
Insert cell
formatValue(2.00400, 'm²', 3, true, true)
Insert cell
parseScaled = (stringValue, scaledQuantity) => {
const s = stringValue.replace(/[\n\r\t]/g,''); // remove control chars
const n = s.includes('.') ? parseFloat(s) : parseInt(s)
return scaledQuantity ? n / scaledQuantity.ratio : n
}
Insert cell
parseScaled2 = (stringValue, siPrefix) => {
const s = stringValue.replace(/[\n\r\t]/g,''); // remove control chars
const n = s.includes('.') ? parseFloat(s) : parseInt(s)
return siPrefix ? n * siPrefix : n
}
Insert cell
insertSpace0 = (s) => {
const n = s.includes('.') ? parseFloat(s) : parseInt(s)
const unit = s.slice((n+ '').length, s.length)
return `${n} ${unit}`
}
Insert cell
insertSpace = (s) => {
const unit = s.replace(/[\d.]/g,'');
const n = s.slice(0, s.length - unit.length)
return `${n} ${unit}`
}
Insert cell
trimTrailingZeros0 = (s) => {
const unit = s.replace(/[\d.]/g,'')
let n = s.slice(0, s.length - unit.length)
if(n.includes('.')){
n = n.replace(/\d0*$/g, '')
n = n.replace(/[.]0*$/g, '')
}
return `${n}${unit}`
}
Insert cell
trimTrailingZeros = (s) => d3.format('~')(s)
Insert cell
n = insertSpace("0.345mΩ")
Insert cell
d3.format('~')(200.000)
Insert cell
'200.00'.replace(/[.]0*$/g,'')
Insert cell
'200.40'.replace(/[.0]*$/g,'')
Insert cell
insertSpace("3.340mΩ")
Insert cell
insertSpace("3.340mΩ")
Insert cell
trimTrailingZeros("3.3400mΩ")
Insert cell
trimTrailingZeros("3.000mΩ")
Insert cell
trimTrailingZeros("300.0")
Insert cell
trimTrailingZeros("300.040")
Insert cell
10e2
Insert cell
1/1000
Insert cell
10e1
Insert cell
n.length
Insert cell
formatScaled(1, ScaledQuantities.MilliOhm, 3, true, true)
Insert cell
formatScaled(1, ScaledQuantities.MilliOhm, 3, false, false, false)
Insert cell
formatScaled(0.00345, ScaledQuantities.MilliOhm, 3, true, false)
Insert cell
formatScaled(0.00345, ScaledQuantities.MilliOhm, 3, false, false)
Insert cell
formatScaled(0.00345, ScaledQuantities.MilliAmp, 3, false, true)
Insert cell
formatScaled(0.00345, ScaledQuantities.CentimeterSquared, 3, false, true)
Insert cell
formatScaled(0.02, ScaledQuantities.CentimeterSquared, 3, false, false)
Insert cell
formatScaled(0.02, '', 3, false, true)
Insert cell
getScaled(0.02, ScaledQuantities.CentimeterSquared)
Insert cell
putScaled(200, ScaledQuantities.CentimeterSquared)
Insert cell
parseScaled('3.45', ScaledQuantities.CentimeterSquared)
Insert cell
parseScaled('0.345', ScaledQuantities.MilliOhm)
Insert cell
parseScaled('345', ScaledQuantities.MilliOhm)
Insert cell
parseScaled2('0.345', 10e-3)
Insert cell
parseScaled2('345', 10e-3)
Insert cell
formatScaled(0.0000345, ScaledQuantities.MicroOhm, 3, true, false)
Insert cell
parseScaled('3.45', ScaledQuantities.MicroOhm)
Insert cell
parseScaled('3.45')
Insert cell
formatScaled(3.45, ScaledQuantities.Centimeter, 3, true, false)
Insert cell
parseScaled('34.5', ScaledQuantities.Centimeter)
Insert cell
f = d3.formatPrefix(",.0", 1e-6)
Insert cell
f(0.00042)
Insert cell
f(0.0042)
Insert cell
fs = new d3.FormatSpecifier({type: "s"})
Insert cell
fs2 = new d3.FormatSpecifier("s")
Insert cell
ts = Object.assign(d3.formatSpecifier("s"), {symbol: ' '})
Insert cell
Insert cell
mjs.evaluate('0.0034 V to mV').toString()
Insert cell
mjs.evaluate('10 meters to cm').toString()
Insert cell
mjs.evaluate('10 meters^2 to cm^2').toString()
Insert cell
mjs.evaluate('0.035 m^2 to cm^2').toString()
Insert cell
mjs.evaluate('0.035 m² to cm²').toString()
Insert cell
mjs.unit(45, 'cm')
Insert cell
mjs.unit(45, 'ohm')
Insert cell
mjs.unit(45, 'ohm').toString()
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more