-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
117 additions
and
33 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
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 |
---|---|---|
@@ -1,19 +1,39 @@ | ||
import { join } from './join'; | ||
|
||
export type FormatMoneyOptions = { | ||
currency?: string; | ||
/** true, if need add sign '+' before positive value */ | ||
withSign?: boolean; | ||
/** minimal number of digits in fractional part */ | ||
minPrecision?: number; | ||
/** maximal number of digits in fractional part */ | ||
maxPrecision?: number; | ||
}; | ||
|
||
/** | ||
* Display money value (with thousand separator) | ||
* @param value money value | ||
* @param currency currency | ||
* @param withSign true, if need add sign '-' before negative value | ||
* @param precision number of digits in fractional part | ||
* @param options format options | ||
*/ | ||
export function formatMoney(value: number, currency: string | undefined = undefined, withSign = false, precision = 2) { | ||
export function formatMoney(value: number, options?: FormatMoneyOptions) { | ||
if (value == null || value == undefined || isNaN(value)) { | ||
return ''; | ||
} | ||
|
||
const currency = options?.currency; | ||
const withSign = options?.withSign ?? false; | ||
const minPrecision = options?.minPrecision ?? 0; | ||
const maxPrecision = options?.maxPrecision ?? 2; | ||
|
||
const sign = withSign && value > 0 ? '+' : ''; | ||
const parts = value.toFixed(precision).split('.'); | ||
const fractionalPart = parts.length > 1 ? '.' + parts[1] : ''; | ||
|
||
return [`${sign}${parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ' ')}${fractionalPart}`, currency] | ||
.filter((x) => x !== undefined) | ||
.join(' '); | ||
const parts = value.toFixed(maxPrecision).split('.'); | ||
let fractionalPart = parts.length > 1 ? parts[1] : undefined; | ||
if (fractionalPart?.match(`0{${maxPrecision}}`)) { | ||
fractionalPart = new Array(minPrecision).fill('0').join(); | ||
} | ||
|
||
const numValue = join([`${sign}${parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, '\u00A0')}`, fractionalPart], '.'); | ||
|
||
return join([numValue, currency], '\u00A0'); | ||
} |
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
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
8cf758f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
money-keeper – ./
money-keeper-ya-erm.vercel.app
money.ya-erm.ru
money-keeper-git-master-ya-erm.vercel.app
my-money-keeper.vercel.app