@@ -142,9 +142,9 @@ extension Web3.Utils {
142
142
return mainPart
143
143
}
144
144
145
- public static func formatToEthereumUnits( _ bigNumber: BigInt , toUnits: Web3 . Utils . Units = . eth, decimals: Int = 4 ) -> String ? {
145
+ public static func formatToEthereumUnits( _ bigNumber: BigInt , toUnits: Web3 . Utils . Units = . eth, decimals: Int = 4 , decimalSeparator : String = " . " ) -> String ? {
146
146
let magnitude = bigNumber. magnitude
147
- guard let formatted = formatToEthereumUnits ( magnitude, toUnits: toUnits, decimals: decimals) else { return nil }
147
+ guard let formatted = formatToEthereumUnits ( magnitude, toUnits: toUnits, decimals: decimals, decimalSeparator : decimalSeparator ) else { return nil }
148
148
switch bigNumber. sign {
149
149
case . plus:
150
150
return formatted
@@ -153,19 +153,48 @@ extension Web3.Utils {
153
153
}
154
154
}
155
155
156
- public static func formatToEthereumUnits( _ bigNumber: BigUInt , toUnits: Web3 . Utils . Units = . eth, decimals: Int = 4 ) -> String ? {
157
- let unitDecimals = toUnits. decimals
158
- var toDecimals = decimals
156
+ public static func formatToPrecision( _ bigNumber: BigInt , numberDecimals: Int = 18 , formattingDecimals: Int = 4 , decimalSeparator: String = " . " ) -> String ? {
157
+ let magnitude = bigNumber. magnitude
158
+ guard let formatted = formatToPrecision ( magnitude, numberDecimals: numberDecimals, formattingDecimals: formattingDecimals, decimalSeparator: decimalSeparator) else { return nil }
159
+ switch bigNumber. sign {
160
+ case . plus:
161
+ return formatted
162
+ case . minus:
163
+ return " - " + formatted
164
+ }
165
+ }
166
+
167
+ public static func formatToEthereumUnits( _ bigNumber: BigUInt , toUnits: Web3 . Utils . Units = . eth, decimals: Int = 4 , decimalSeparator: String = " . " , fallbackToScientific: Bool = false ) -> String ? {
168
+ return formatToPrecision ( bigNumber, numberDecimals: toUnits. decimals, formattingDecimals: decimals, decimalSeparator: decimalSeparator, fallbackToScientific: fallbackToScientific) ;
169
+ }
170
+
171
+ public static func formatToPrecision( _ bigNumber: BigUInt , numberDecimals: Int = 18 , formattingDecimals: Int = 4 , decimalSeparator: String = " . " , fallbackToScientific: Bool = false ) -> String ? {
172
+ let unitDecimals = numberDecimals
173
+ var toDecimals = formattingDecimals
159
174
if unitDecimals < toDecimals {
160
175
toDecimals = unitDecimals
161
176
}
162
177
let divisor = BigUInt ( 10 ) . power ( unitDecimals)
163
178
let ( quotient, remainder) = bigNumber. quotientAndRemainder ( dividingBy: divisor)
164
- let remainderPadded = String ( remainder) . leftPadding ( toLength: unitDecimals, withPad: " 0 " ) [ 0 ..< toDecimals]
165
- if ( decimals == 0 ) {
179
+ let fullRemainder = String ( remainder) ;
180
+ let fullPaddedRemainder = fullRemainder. leftPadding ( toLength: unitDecimals, withPad: " 0 " )
181
+ let remainderPadded = fullPaddedRemainder [ 0 ..< toDecimals]
182
+ if remainderPadded == String ( repeating: " 0 " , count: toDecimals) && quotient == 0 {
183
+ var firstDigit = 0
184
+ for char in fullPaddedRemainder {
185
+ if ( char == " 0 " ) {
186
+ firstDigit = firstDigit + 1 ;
187
+ } else {
188
+ firstDigit = firstDigit + 1 ;
189
+ break
190
+ }
191
+ }
192
+ return fullRemainder + " e- " + String( firstDigit)
193
+ }
194
+ if ( toDecimals == 0 ) {
166
195
return String ( quotient)
167
196
}
168
- return String ( quotient) + " . " + remainderPadded
197
+ return String ( quotient) + decimalSeparator + remainderPadded
169
198
}
170
199
171
200
static public func personalECRecover( _ personalMessage: String , signature: String ) -> EthereumAddress ? {
0 commit comments