Skip to content

Commit

Permalink
feat: add currencyFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 30, 2018
1 parent bf9a1e4 commit 72b1d59
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 900 deletions.
46 changes: 46 additions & 0 deletions src/currencyFormat.ts
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)
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as Disposer } from './Disposer'
export { default as base64Decode } from './base64Decode'
export { default as base64Encode } from './base64Encode'
export { default as base64UrlDecode } from './base64UrlDecode'
Expand All @@ -6,7 +7,7 @@ export { default as bindEvent } from './bindEvent'
export { default as castArray } from './castArray'
export { default as clamp } from './clamp'
export { default as cssTransform } from './cssTransform'
export { default as Disposer } from './Disposer'
export { default as currencyFormat } from './currencyFormat'
export { default as fill } from './fill'
export { default as forOwn } from './forOwn'
export { default as getType } from './getType'
Expand Down
33 changes: 33 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,3 +759,36 @@ describe('isPromise', () => {
expect(vtils.isPromise(/ddd/)).toBeFalsy()
})
})

describe('currencyFormat', () => {
test('默认选项:千分位逗号分隔,保留两位小数,四舍五入', () => {
expect(vtils.currencyFormat(20.1)).toBe('20.10')
expect(vtils.currencyFormat(2320.121)).toBe('2,320.12')
expect(vtils.currencyFormat(2320.126)).toBe('2,320.13')
expect(vtils.currencyFormat(840224)).toBe('840,224.00')
})
test('禁用千分位逗号分隔', () => {
expect(vtils.currencyFormat(20.1, { thousands: false })).toBe('20.10')
expect(vtils.currencyFormat(2320.121, { thousands: false })).toBe('2320.12')
expect(vtils.currencyFormat(2320.126, { thousands: false })).toBe('2320.13')
expect(vtils.currencyFormat(840224, { thousands: false })).toBe('840224.00')
})
test('保留 3 位小数', () => {
expect(vtils.currencyFormat(20.1, { decimalDigits: 3 })).toBe('20.100')
expect(vtils.currencyFormat(2320.121, { decimalDigits: 3 })).toBe('2,320.121')
expect(vtils.currencyFormat(2320.126, { decimalDigits: 3 })).toBe('2,320.126')
expect(vtils.currencyFormat(840224, { decimalDigits: 3 })).toBe('840,224.000')
})
test('不保留小数', () => {
expect(vtils.currencyFormat(20.1, { decimalDigits: 0 })).toBe('20')
expect(vtils.currencyFormat(2320.121, { decimalDigits: 0 })).toBe('2,320')
expect(vtils.currencyFormat(2320.126, { decimalDigits: 0 })).toBe('2,320')
expect(vtils.currencyFormat(840224, { decimalDigits: 0 })).toBe('840,224')
})
test('不处理小数', () => {
expect(vtils.currencyFormat(20.1, { decimal: false })).toBe('20.1')
expect(vtils.currencyFormat(2320.121, { decimal: false })).toBe('2,320.121')
expect(vtils.currencyFormat(2320.126, { decimal: false })).toBe('2,320.126')
expect(vtils.currencyFormat(840224, { decimal: false })).toBe('840,224')
})
})
Loading

0 comments on commit 72b1d59

Please sign in to comment.