Add display of units for calculated KCL values (#7619)
* Add display of units in UI modals with calculated KCL values * Fix command bar display to handle units * Add display of units in the command bar * Fix more cases of NaN from units * Fix to support explicit plus for exponent in scientific notation * Fix display in autocomplete * Change to parseFloat to be more resilient * Add e2e test for command bar * Change an existing test to use explicit inline units * Fix case when input string can't be parsed
This commit is contained in:
@ -328,6 +328,32 @@ export function roundOff(num: number, precision: number = 2): number {
|
||||
return Math.round(num * x) / x
|
||||
}
|
||||
|
||||
export function roundOffWithUnits(
|
||||
numWithUnits: string,
|
||||
precision: number = 2
|
||||
): string {
|
||||
const match = numWithUnits.match(
|
||||
/^([+-]?[\d.]+(?:[eE][+-]?\d+)?)([a-zA-Z_?]+)$/
|
||||
)
|
||||
let num: string
|
||||
let suffix: string
|
||||
if (match) {
|
||||
num = match[1]
|
||||
suffix = match[2] ?? ''
|
||||
} else {
|
||||
// If no match, assume it's just a number with no units.
|
||||
num = numWithUnits
|
||||
suffix = ''
|
||||
}
|
||||
const parsedNum = parseFloat(num)
|
||||
if (Number.isNaN(parsedNum)) {
|
||||
// If parsing fails, return the original string.
|
||||
return numWithUnits
|
||||
}
|
||||
const roundedNum = roundOff(parsedNum, precision)
|
||||
return `${roundedNum}${suffix}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the number as a string has any precision in the decimal places
|
||||
* '1' -> 0
|
||||
|
Reference in New Issue
Block a user