-
Notifications
You must be signed in to change notification settings - Fork 22
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
4 changed files
with
81 additions
and
900 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
export type CurrencyFormatValue = number | string | ||
|
||
export interface CurrencyFormatOptions { | ||
thousands?: boolean, | ||
decimal?: boolean, | ||
decimalDigits?: number | ||
} | ||
|
||
const defaultOptions: CurrencyFormatOptions = { | ||
thousands: true, | ||
decimal: true, | ||
decimalDigits: 2 | ||
} | ||
|
||
/** | ||
* 货币值格式化。 | ||
* | ||
* @param value 要格式化的值 | ||
* @param options 选项 | ||
* @returns 格式化后的值 | ||
*/ | ||
export default function currencyFormat(value: CurrencyFormatValue, options?: CurrencyFormatOptions): string { | ||
value = Number(value) | ||
options = { | ||
...defaultOptions, | ||
...options | ||
} | ||
if (options.decimal) { | ||
value = value.toFixed(options.decimalDigits) | ||
} | ||
if (options.thousands) { | ||
let [integer, decimal = ''] = value.toString().split('.') // tslint:disable-line | ||
value = '' | ||
while (integer.length > 3) { | ||
value = `,${integer.slice(-3)}${value}` | ||
integer = integer.slice(0, integer.length - 3) | ||
} | ||
if (integer) { | ||
value = integer + value | ||
} | ||
if (decimal) { | ||
value += `.${decimal}` | ||
} | ||
} | ||
return String(value) | ||
} |
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
Oops, something went wrong.