@@ -36,7 +36,9 @@ const stringFormatters = () => {
36
36
return `0${ unit } `
37
37
}
38
38
39
- return Math . max ( value , Math . pow ( 10 , - fractionDigits ) )
39
+ const op = value < 0 ? Math . min : Math . max
40
+
41
+ return op ( value , Math . pow ( 10 , - fractionDigits ) )
40
42
. toFixed ( fractionDigits ) + unit
41
43
} ,
42
44
@@ -85,19 +87,21 @@ const stringFormatters = () => {
85
87
* Formats a number representing mm to human readable distance.
86
88
*/
87
89
getReadableLengthString : ( lengthInMm : number , options ?: { showMicrons ?: boolean , showKilometers ?: boolean } , fractionDigits : number | undefined = undefined ) => {
88
- if ( lengthInMm >= 1000000 && options ?. showKilometers ) {
90
+ const absLengthInMm = Math . abs ( lengthInMm )
91
+
92
+ if ( absLengthInMm >= 1000000 && options ?. showKilometers ) {
89
93
return ( lengthInMm / 1000000 ) . toFixed ( fractionDigits ?? 2 ) + ' km'
90
94
}
91
95
92
- if ( lengthInMm >= 1000 ) {
96
+ if ( absLengthInMm >= 1000 ) {
93
97
return ( lengthInMm / 1000 ) . toFixed ( fractionDigits ?? 2 ) + ' m'
94
98
}
95
99
96
- if ( lengthInMm > 100 ) {
100
+ if ( absLengthInMm > 100 ) {
97
101
return ( lengthInMm / 10 ) . toFixed ( fractionDigits ?? 1 ) + ' cm'
98
102
}
99
103
100
- if ( lengthInMm < 0.1 && options ?. showMicrons ) {
104
+ if ( absLengthInMm < 0.1 && options ?. showMicrons ) {
101
105
return instance . getStringValueWithUnit ( lengthInMm * 1000 , fractionDigits ?? 0 , ' μm' )
102
106
}
103
107
@@ -108,7 +112,9 @@ const stringFormatters = () => {
108
112
* Formats a number representing g to human readable weight.
109
113
*/
110
114
getReadableWeightString : ( weightInG : number , fractionDigits = 2 ) => {
111
- if ( weightInG >= 1000 ) {
115
+ const absWeightInG = Math . abs ( weightInG )
116
+
117
+ if ( absWeightInG >= 1000 ) {
112
118
return ( weightInG / 1000 ) . toFixed ( fractionDigits ) + ' kg'
113
119
}
114
120
@@ -120,7 +126,7 @@ const stringFormatters = () => {
120
126
*/
121
127
getReadableFrequencyString : ( frequencyInHz : number , fractionDigits = 0 ) => {
122
128
let i = 0
123
- while ( frequencyInHz >= 1000 ) {
129
+ while ( Math . abs ( frequencyInHz ) >= 1000 ) {
124
130
frequencyInHz = frequencyInHz / 1000
125
131
i ++
126
132
}
@@ -133,7 +139,7 @@ const stringFormatters = () => {
133
139
*/
134
140
getReadableResistanceString : ( resistanceInOhms : number , fractionDigits = 1 ) => {
135
141
let i = 0
136
- while ( resistanceInOhms >= 1000 ) {
142
+ while ( Math . abs ( resistanceInOhms ) >= 1000 ) {
137
143
resistanceInOhms = resistanceInOhms / 1000
138
144
i ++
139
145
}
0 commit comments