Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fractionless): honor decimals in fractionless amounts #265

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ export declare class Money {
*
* Example:
* Money.fromFractionlessAmount(1000, 'NOK') => 10.00 NOK
* Money.fromFractionlessAmount(1000, 'NOK', { decimals: 3 }) => 1.000 NOK
*/
static fromFractionlessAmount(
amount: number,
Expand Down Expand Up @@ -314,7 +315,8 @@ export declare class Money {

// Converters
/**
* Converts the money amount into a whole number given in the minor unit of the currency
* Converts the money amount into a whole number given in the minor unit of the currency.
* Honors the current precision in use.
*/
toFractionlessAmount: () => number;
/**
Expand Down
26 changes: 26 additions & 0 deletions src/__tests__/money.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,32 @@ describe("money", () => {
expect(result).toBe("10.00");
});

it("should instantiate from fractionless amount and honor decimals in options", () => {
const result = Money.fromFractionlessAmount(1000, "NOK", {
decimals: 3,
}).toString();
expect(result).toBe("1.000");
});

it("should convert to fractionless amount", () => {
const result = Money.of(10, "NOK").toFractionlessAmount();
expect(result).toBe(1000);
});

it("should convert from fractionless amount to fractionless amount and honor decimals in options", () => {
const result = Money.fromFractionlessAmount(1000, "NOK", {
decimals: 3,
}).toFractionlessAmount();
expect(result).toBe(1000);
});

it("should convert from normal to fractionless amount and honor decimals in options", () => {
const result = Money.of(10, "NOK", {
decimals: 4,
}).toFractionlessAmount();
expect(result).toBe(100000);
});

it("should print in locale", () => {
const result = Money.of(5.5, "NOK").toLocaleString("no-NB");
expect(result).toBe("5,50");
Expand Down
13 changes: 8 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ export class Money {
*
* Example:
* Money.fromFractionlessAmount(1000, 'NOK') => 10.00 NOK
* Money.fromFractionlessAmount(1000, 'NOK', { decimals: 3 }) => 1.000 NOK
*/
static fromFractionlessAmount(
amount: number,
currency: string,
options?: AdditionalOptions,
): Money {
return Money.of(amount, currency, options).divide(
10 ** currencyToDecimals(currency),
);
const decimals = options?.decimals ?? currencyToDecimals(currency);
return Money.of(amount, currency, options).divide(10 ** decimals);
}

/**
Expand Down Expand Up @@ -309,10 +309,13 @@ export class Money {
};

/**
* Converts the money amount into a whole number given in the minor unit of the currency
* Converts the money amount into a whole number given in the minor unit of the currency.
* Honors the current precision in use.
*/
toFractionlessAmount = (): number => {
return this.multiply(10 ** currencyToDecimals(this.currency()))
const decimals =
this.getDecimals() ?? currencyToDecimals(this.currency());
return this.multiply(10 ** decimals)
.round(0)
.toNumber();
};
Expand Down