@@ -675,8 +675,8 @@ extension Web3.Utils {
675675 return formatToPrecision ( bigNumber, numberDecimals: toUnits. decimals, formattingDecimals: decimals, decimalSeparator: decimalSeparator, fallbackToScientific: fallbackToScientific) ;
676676 }
677677
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.
680680 /// Fallbacks to scientific format if higher precision is required.
681681 ///
682682 /// Returns nil of formatting is not possible to satisfy.
@@ -691,7 +691,7 @@ extension Web3.Utils {
691691 }
692692 let divisor = BigUInt ( 10 ) . power ( unitDecimals)
693693 let ( quotient, remainder) = bigNumber. quotientAndRemainder ( dividingBy: divisor)
694- let fullRemainder = String ( remainder) ;
694+ var fullRemainder = String ( remainder) ;
695695 let fullPaddedRemainder = fullRemainder. leftPadding ( toLength: unitDecimals, withPad: " 0 " )
696696 let remainderPadded = fullPaddedRemainder [ 0 ..< toDecimals]
697697 if remainderPadded == String ( repeating: " 0 " , count: toDecimals) {
@@ -703,6 +703,18 @@ extension Web3.Utils {
703703 if ( char == " 0 " ) {
704704 firstDigit = firstDigit + 1 ;
705705 } 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
706718 firstDigit = firstDigit + 1 ;
707719 break
708720 }
0 commit comments