|
| 1 | +/** |
| 2 | + * JavaScript version of Maikuolan\Common\NumberFormatter's format method. |
| 3 | + * Formats the supplied number according to definitions. |
| 4 | + * |
| 5 | + * @param {any} Number The number to format (int, float, string, etc). |
| 6 | + * @param {number} Decimals The number of decimal places (optional). |
| 7 | + * @returns {string} The formatted number, or an empty string on failure. |
| 8 | + */ |
| 9 | +function nft(Number, Decimals = 0) { |
| 10 | + let ConversionSet = JSON.parse('%s'); |
| 11 | + let GroupSeparator = '%s'; |
| 12 | + let GroupSize = %s; |
| 13 | + let GroupOffset = %s; |
| 14 | + let DecimalSeparator = '%s'; |
| 15 | + let Base = %s; |
| 16 | + if (Base < 2 || Base > 36) { |
| 17 | + return ''; |
| 18 | + } |
| 19 | + var DecPos = String(Number).indexOf('.'); |
| 20 | + let Fraction; |
| 21 | + if (DecPos !== -1) { |
| 22 | + if (Decimals > 0 && DecimalSeparator && !ConversionSet['.']) { |
| 23 | + Fraction = (String(Number).substring(DecPos + 1)) || ''; |
| 24 | + let Len = Fraction.length; |
| 25 | + if (Len > 0) { |
| 26 | + Fraction = convertFraction(Fraction, 10, Base, Decimals); |
| 27 | + Fraction = Fraction.substring(0, Decimals); |
| 28 | + Len = Fraction.length; |
| 29 | + } |
| 30 | + if (Len < Decimals) { |
| 31 | + Fraction += '0'.repeat(Decimals - Len); |
| 32 | + } |
| 33 | + } |
| 34 | + Number = String(Number).substring(0, DecPos); |
| 35 | + } else { |
| 36 | + Number = String(Number); |
| 37 | + } |
| 38 | + if (Base !== 10) { |
| 39 | + Number = parseInt(Number).toString(Base); |
| 40 | + } |
| 41 | + let Formatted = ''; |
| 42 | + let WholeLen = -1; |
| 43 | + if (typeof ConversionSet['=' + Number] != 'undefined') { |
| 44 | + Formatted = ConversionSet['=' + Number]; |
| 45 | + WholeLen = -1; |
| 46 | + } else { |
| 47 | + WholeLen = Number.length; |
| 48 | + } |
| 49 | + let ThouPos = GroupOffset; |
| 50 | + for (let Unit = 0, Pos = WholeLen - 1; Pos > -1; Pos--, Unit++) { |
| 51 | + if (ThouPos >= GroupSize) { |
| 52 | + ThouPos = 1; |
| 53 | + Formatted = GroupSeparator + Formatted; |
| 54 | + } else { |
| 55 | + ThouPos++; |
| 56 | + } |
| 57 | + var Key = Number.substring(Pos, Pos + 1); |
| 58 | + var Double = (Pos > 0) ? Number.substring(Pos - 1, Pos) + Key : ''; |
| 59 | + let Power = ''; |
| 60 | + let Digit = ''; |
| 61 | + if (typeof ConversionSet['^' + Unit + '+' + Double] != 'undefined') { |
| 62 | + Digit = ConversionSet['^' + Unit + '+' + Double]; |
| 63 | + } else if (typeof ConversionSet['^' + Unit + '+' + Key] != 'undefined') { |
| 64 | + Digit = ConversionSet['^' + Unit + '+' + Key]; |
| 65 | + } else if (typeof ConversionSet['+' + Key] != 'undefined') { |
| 66 | + Digit = ConversionSet['+' + Key]; |
| 67 | + } else { |
| 68 | + Digit = (typeof ConversionSet[Key] != 'undefined') ? ConversionSet[Key] : Key; |
| 69 | + if (typeof ConversionSet['^' + Unit + '*' + Key] != 'undefined') { |
| 70 | + Power = ConversionSet['^' + Unit + '*' + Key]; |
| 71 | + } else if (typeof ConversionSet['^' + Unit] != 'undefined') { |
| 72 | + Power = ConversionSet['^' + Unit]; |
| 73 | + } |
| 74 | + } |
| 75 | + Formatted = Digit + Power + Formatted; |
| 76 | + } |
| 77 | + if (Fraction && Decimals && DecimalSeparator && !ConversionSet['.']) { |
| 78 | + Formatted += DecimalSeparator; |
| 79 | + for (let Pos = 0, Len = Fraction.length; Pos < Len; Pos++) { |
| 80 | + var Key = Fraction.substring(Pos, Pos + 1); |
| 81 | + let Power = ''; |
| 82 | + let Digit = ''; |
| 83 | + if (typeof ConversionSet['^-' + Pos + '+' + Key] != 'undefined') { |
| 84 | + Digit = ConversionSet['^-' + Pos + '+' + Key]; |
| 85 | + } else if (typeof ConversionSet['-+' + Key] != 'undefined') { |
| 86 | + Digit = ConversionSet['-+' + Key]; |
| 87 | + } else { |
| 88 | + if (typeof ConversionSet['-' + Key] != 'undefined') { |
| 89 | + Digit = ConversionSet['-' + Key]; |
| 90 | + } else { |
| 91 | + Digit = (typeof ConversionSet[Key] != 'undefined') ? ConversionSet[Key] : Key; |
| 92 | + } |
| 93 | + if (typeof ConversionSet['^-' + Pos + '*' + Key] != 'undefined') { |
| 94 | + Power = ConversionSet['^-' + Pos + '*' + Key]; |
| 95 | + } else if (typeof ConversionSet['^-' + Pos] != 'undefined') { |
| 96 | + Power = ConversionSet['^-' + Pos]; |
| 97 | + } |
| 98 | + } |
| 99 | + Formatted += Digit + Power; |
| 100 | + } |
| 101 | + } |
| 102 | + var DecLen = DecimalSeparator.length; |
| 103 | + if (DecLen && Formatted.substring(0, DecLen) === DecimalSeparator) { |
| 104 | + Formatted = Formatted.substring(DecLen); |
| 105 | + } |
| 106 | + return Formatted; |
| 107 | +} |
0 commit comments