Browse Source

Fix millia

Use special millions prefix only when millia is less than one.
master
TheoryOfNekomata 1 year ago
parent
commit
f2e4ab3c5c
2 changed files with 19 additions and 12 deletions
  1. +16
    -12
      packages/core/src/systems/en-US/short-count/stringify.ts
  2. +3
    -0
      packages/core/test/systems/en-US.test.ts

+ 16
- 12
packages/core/src/systems/en-US/short-count/stringify.ts View File

@@ -79,11 +79,11 @@ const makeHundredsName = (hundreds: number, tens: number, ones: number, addTensD
/**
* Builds a name for numbers in the millions.
* @param millions - Millions digit.
* @param milliaCount - Number of millia- groups.
* @param longestMilliaCount - Number of millia- groups.
* @returns string The millions prefix.
*/
const makeMillionsPrefix = (millions: number, milliaCount: GroupPlace) => {
if (milliaCount > 0) {
const makeMillionsPrefix = (millions: number, longestMilliaCount: GroupPlace) => {
if (longestMilliaCount > 0) {
return MILLIONS_PREFIXES[millions] as MillionsPrefix;
}

@@ -94,12 +94,16 @@ const makeMillionsPrefix = (millions: number, milliaCount: GroupPlace) => {
* Builds a name for numbers in the decillions.
* @param decillions - Decillions digit.
* @param millions - Millions digit.
* @param milliaCount - Number of millia- groups.
* @param longestMilliaCount - Number of millia- groups.
* @returns string The decillions prefix.
*/
const makeDecillionsPrefix = (decillions: number, millions: number, milliaCount: GroupPlace) => {
const makeDecillionsPrefix = (
decillions: number,
millions: number,
longestMilliaCount: GroupPlace,
) => {
if (decillions === 0) {
return makeMillionsPrefix(millions, milliaCount);
return makeMillionsPrefix(millions, longestMilliaCount);
}

const onesPrefix = MILLIONS_PREFIXES[millions] as MillionsPrefix;
@@ -112,17 +116,17 @@ const makeDecillionsPrefix = (decillions: number, millions: number, milliaCount:
* @param centillions - Centillions digit.
* @param decillions - Decillions digit.
* @param millions - Millions digit.
* @param milliaCount - Number of millia- groups.
* @param longestMilliaCount - Number of millia- groups.
* @returns string The centillions prefix.
*/
const makeCentillionsPrefix = (
centillions: number,
decillions: number,
millions: number,
milliaCount: GroupPlace,
longestMilliaCount: GroupPlace,
) => {
if (centillions === 0) {
return makeDecillionsPrefix(decillions, millions, milliaCount);
return makeDecillionsPrefix(decillions, millions, longestMilliaCount);
}

const onesPrefix = MILLIONS_PREFIXES[millions] as MillionsPrefix;
@@ -177,10 +181,10 @@ const getGroupName = (place: GroupPlace, shortenMillia: boolean) => {
)
.map(([groupDigits, groupPlace]) => [groupDigits.padStart(3, '0'), groupPlace] as const)
.filter(([groupDigits]) => groupDigits !== EMPTY_GROUP_DIGITS)
.map(([groupDigits, groupPlace]) => {
.map(([groupDigits, groupPlace], _index, millias) => {
const [hundreds, tens, ones] = groupDigits.split('').map(Number);
if (groupPlace < 1) {
return makeCentillionsPrefix(hundreds, tens, ones, groupPlace);
return makeCentillionsPrefix(hundreds, tens, ones, BigInt(millias.length - 1));
}

const milliaSuffix = (
@@ -193,7 +197,7 @@ const getGroupName = (place: GroupPlace, shortenMillia: boolean) => {
return milliaSuffix;
}

return makeCentillionsPrefix(hundreds, tens, ones, groupPlace) + milliaSuffix;
return makeCentillionsPrefix(hundreds, tens, ones, BigInt(millias.length - 1)) + milliaSuffix;
})
.join('');



+ 3
- 0
packages/core/test/systems/en-US.test.ts View File

@@ -282,5 +282,8 @@ describe('numerica', () => {
const exp4 = numberToExponential(value4);
expect(stringify(value4)).toBe('twelve thousand twenty');
expect(parse(stringify(value4))).toBe(exp4);

const value5 = '1e3006';
expect(stringify(value5)).toBe('one milliauntillion');
});
});

Loading…
Cancel
Save