@@ -675,8 +675,8 @@ extension Web3.Utils {
675
675
return formatToPrecision ( bigNumber, numberDecimals: toUnits. decimals, formattingDecimals: decimals, decimalSeparator: decimalSeparator, fallbackToScientific: fallbackToScientific) ;
676
676
}
677
677
678
- /// Formats a BigUInt object to String. The supplied number is first divided into integer and decimal part based on "toUnits ",
679
- /// then limit the decimal part to "decimals " symbols and uses a "decimalSeparator" as a separator.
678
+ /// Formats a BigUInt object to String. The supplied number is first divided into integer and decimal part based on "numberDecimals ",
679
+ /// then limits the decimal part to "formattingDecimals " symbols and uses a "decimalSeparator" as a separator.
680
680
/// Fallbacks to scientific format if higher precision is required.
681
681
///
682
682
/// Returns nil of formatting is not possible to satisfy.
@@ -691,7 +691,7 @@ extension Web3.Utils {
691
691
}
692
692
let divisor = BigUInt ( 10 ) . power ( unitDecimals)
693
693
let ( quotient, remainder) = bigNumber. quotientAndRemainder ( dividingBy: divisor)
694
- let fullRemainder = String ( remainder) ;
694
+ var fullRemainder = String ( remainder) ;
695
695
let fullPaddedRemainder = fullRemainder. leftPadding ( toLength: unitDecimals, withPad: " 0 " )
696
696
let remainderPadded = fullPaddedRemainder [ 0 ..< toDecimals]
697
697
if remainderPadded == String ( repeating: " 0 " , count: toDecimals) {
@@ -703,6 +703,18 @@ extension Web3.Utils {
703
703
if ( char == " 0 " ) {
704
704
firstDigit = firstDigit + 1 ;
705
705
} else {
706
+ let firstDecimalUnit = String ( fullPaddedRemainder [ firstDigit ..< firstDigit+ 1 ] )
707
+ var remainingDigits = " "
708
+ let numOfRemainingDecimals = fullPaddedRemainder. count - firstDigit - 1
709
+ if numOfRemainingDecimals <= 0 {
710
+ remainingDigits = " "
711
+ } else if numOfRemainingDecimals > formattingDecimals {
712
+ let end = firstDigit+ 1 + formattingDecimals > fullPaddedRemainder. count ? fullPaddedRemainder. count : firstDigit+ 1 + formattingDecimals
713
+ remainingDigits = String ( fullPaddedRemainder [ firstDigit+ 1 ..< end] )
714
+ } else {
715
+ remainingDigits = String ( fullPaddedRemainder [ firstDigit+ 1 ..< fullPaddedRemainder. count] )
716
+ }
717
+ fullRemainder = firstDecimalUnit + decimalSeparator + remainingDigits
706
718
firstDigit = firstDigit + 1 ;
707
719
break
708
720
}
0 commit comments