-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changelog excerpt: - Some of the more unusual available number formatting choices (e.g., choices not using base-10 or Arabic numerals) didn't mesh well with the JavaScript code responsible for using them; Fixed.
- Loading branch information
Showing
6 changed files
with
129 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/** | ||
* JavaScript version of Maikuolan\Common\NumberFormatter's format method. | ||
* Formats the supplied number according to definitions. | ||
* | ||
* @param {any} Number The number to format (int, float, string, etc). | ||
* @param {number} Decimals The number of decimal places (optional). | ||
* @returns {string} The formatted number, or an empty string on failure. | ||
*/ | ||
function nft(Number, Decimals = 0) { | ||
let ConversionSet = JSON.parse('%s'); | ||
let GroupSeparator = '%s'; | ||
let GroupSize = %s; | ||
let GroupOffset = %s; | ||
let DecimalSeparator = '%s'; | ||
let Base = %s; | ||
if (Base < 2 || Base > 36) { | ||
return ''; | ||
} | ||
var DecPos = String(Number).indexOf('.'); | ||
let Fraction; | ||
if (DecPos !== -1) { | ||
if (Decimals > 0 && DecimalSeparator && !ConversionSet['.']) { | ||
Fraction = (String(Number).substring(DecPos + 1)) || ''; | ||
let Len = Fraction.length; | ||
if (Len > 0) { | ||
Fraction = convertFraction(Fraction, 10, Base, Decimals); | ||
Fraction = Fraction.substring(0, Decimals); | ||
Len = Fraction.length; | ||
} | ||
if (Len < Decimals) { | ||
Fraction += '0'.repeat(Decimals - Len); | ||
} | ||
} | ||
Number = String(Number).substring(0, DecPos); | ||
} else { | ||
Number = String(Number); | ||
} | ||
if (Base !== 10) { | ||
Number = parseInt(Number).toString(Base); | ||
} | ||
let Formatted = ''; | ||
let WholeLen = -1; | ||
if (typeof ConversionSet['=' + Number] != 'undefined') { | ||
Formatted = ConversionSet['=' + Number]; | ||
WholeLen = -1; | ||
} else { | ||
WholeLen = Number.length; | ||
} | ||
let ThouPos = GroupOffset; | ||
for (let Unit = 0, Pos = WholeLen - 1; Pos > -1; Pos--, Unit++) { | ||
if (ThouPos >= GroupSize) { | ||
ThouPos = 1; | ||
Formatted = GroupSeparator + Formatted; | ||
} else { | ||
ThouPos++; | ||
} | ||
var Key = Number.substring(Pos, Pos + 1); | ||
var Double = (Pos > 0) ? Number.substring(Pos - 1, Pos) + Key : ''; | ||
let Power = ''; | ||
let Digit = ''; | ||
if (typeof ConversionSet['^' + Unit + '+' + Double] != 'undefined') { | ||
Digit = ConversionSet['^' + Unit + '+' + Double]; | ||
} else if (typeof ConversionSet['^' + Unit + '+' + Key] != 'undefined') { | ||
Digit = ConversionSet['^' + Unit + '+' + Key]; | ||
} else if (typeof ConversionSet['+' + Key] != 'undefined') { | ||
Digit = ConversionSet['+' + Key]; | ||
} else { | ||
Digit = (typeof ConversionSet[Key] != 'undefined') ? ConversionSet[Key] : Key; | ||
if (typeof ConversionSet['^' + Unit + '*' + Key] != 'undefined') { | ||
Power = ConversionSet['^' + Unit + '*' + Key]; | ||
} else if (typeof ConversionSet['^' + Unit] != 'undefined') { | ||
Power = ConversionSet['^' + Unit]; | ||
} | ||
} | ||
Formatted = Digit + Power + Formatted; | ||
} | ||
if (Fraction && Decimals && DecimalSeparator && !ConversionSet['.']) { | ||
Formatted += DecimalSeparator; | ||
for (let Pos = 0, Len = Fraction.length; Pos < Len; Pos++) { | ||
var Key = Fraction.substring(Pos, Pos + 1); | ||
let Power = ''; | ||
let Digit = ''; | ||
if (typeof ConversionSet['^-' + Pos + '+' + Key] != 'undefined') { | ||
Digit = ConversionSet['^-' + Pos + '+' + Key]; | ||
} else if (typeof ConversionSet['-+' + Key] != 'undefined') { | ||
Digit = ConversionSet['-+' + Key]; | ||
} else { | ||
if (typeof ConversionSet['-' + Key] != 'undefined') { | ||
Digit = ConversionSet['-' + Key]; | ||
} else { | ||
Digit = (typeof ConversionSet[Key] != 'undefined') ? ConversionSet[Key] : Key; | ||
} | ||
if (typeof ConversionSet['^-' + Pos + '*' + Key] != 'undefined') { | ||
Power = ConversionSet['^-' + Pos + '*' + Key]; | ||
} else if (typeof ConversionSet['^-' + Pos] != 'undefined') { | ||
Power = ConversionSet['^-' + Pos]; | ||
} | ||
} | ||
Formatted += Digit + Power; | ||
} | ||
} | ||
var DecLen = DecimalSeparator.length; | ||
if (DecLen && Formatted.substring(0, DecLen) === DecimalSeparator) { | ||
Formatted = Formatted.substring(DecLen); | ||
} | ||
return Formatted; | ||
} | ||
|
||
// https://chat.openai.com/c/2df8e967-edd7-471a-9eb5-23044aff6819 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters