Skip to content

Commit

Permalink
v0.7.0: Fix Bug related to IEEE 754 (#16)
Browse files Browse the repository at this point in the history
* Partially Fix IEEE 754 Bug

* Add few more test cases and fix string

* Fix for Big Numbers

* Improve performance for Number

* Optimize Epsilon

* Add tests

* v0.7.0

Co-authored-by: Manassarn Manoonchai <manassarn@gmail.com>
  • Loading branch information
leomotors and narze authored Mar 26, 2022
1 parent 6db1aa2 commit dc1b1f8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.6.1",
"version": "0.7.0",
"name": "baht",
"author": "Manassarn Manoonchai",
"description": "Convert number to Thai Baht format, but faster & fully typed.",
Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export function convert(input: number | string): string | boolean {
input = -input;
}
baht = Math.floor(input);
satang = Number.isInteger(input) ? 0 : Math.floor((input * 100) % 100);
satang = Number.isInteger(input)
? 0
: Math.floor(((input + Number.EPSILON * (baht || 1)) * 100) % 100);
bahtStr = '' + baht;
} else if (typeof input === 'string') {
let negativeLeadingZeroPattern = /^-0+/;
Expand Down Expand Up @@ -90,7 +92,10 @@ export function convert(input: number | string): string | boolean {
bahtStr = inputStr.slice(0, periodIdx);
baht = +bahtStr;
satangStr = inputStr.slice(periodIdx + 1);
satang = satangStr ? Math.floor(Number('0.' + satangStr) * 100) : 0;
satang = satangStr
? Number(satangStr.slice(0, 2)) *
(satangStr.length >= 2 ? 1 : [100, 10][satangStr.length])
: 0;
} else {
baht = inputNum;
bahtStr = inputStr;
Expand Down
68 changes: 68 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ describe('convert', () => {
expect(convert((true as unknown) as number)).toBe(false);
expect(convert(({} as unknown) as number)).toBe(false);
expect(convert(([] as unknown) as number)).toBe(false);
expect(convert(('155233.4b6' as unknown) as number)).toBe(false);
expect(convert(('155233.476a85' as unknown) as number)).toBe(false);
});

it('should be a function', () => {
Expand Down Expand Up @@ -102,6 +104,23 @@ describe('convert', () => {
expect(convert(-121)).toEqual('ลบหนึ่งร้อยยี่สิบเอ็ดบาทถ้วน');
});

it('should convert 1-99 satangs correctly compared to baht', () => {
const bahtArray = [];
const satangArray = [];

for (let i = 1; i < 100; i++) {
bahtArray.push((convert(i) as string).replace('บาทถ้วน', ''));
satangArray.push(
(convert(+`0.${i.toString().padStart(2, '0')}`) as string).replace(
'สตางค์',
''
)
);
}

expect(bahtArray).toEqual(satangArray);
});

it('should convert big number to Baht', () => {
expect(convert(1000000)).toEqual('หนึ่งล้านบาทถ้วน');
expect(convert(1000001)).toEqual('หนึ่งล้านเอ็ดบาทถ้วน');
Expand Down Expand Up @@ -145,6 +164,9 @@ describe('convert', () => {
expect(convert(30034567.0)).toEqual(
'สามสิบล้านสามหมื่นสี่พันห้าร้อยหกสิบเจ็ดบาทถ้วน'
);
expect(convert(1534325986.4336942)).toEqual(
'หนึ่งพันห้าร้อยสามสิบสี่ล้านสามแสนสองหมื่นห้าพันเก้าร้อยแปดสิบหกบาทสี่สิบสามสตางค์'
);
});

it('should convert number to Baht with Satang', () => {
Expand Down Expand Up @@ -177,6 +199,12 @@ describe('convert', () => {
expect(convert(('-1654321.21' as unknown) as number)).toBe(
'ลบหนึ่งล้านหกแสนห้าหมื่นสี่พันสามร้อยยี่สิบเอ็ดบาทยี่สิบเอ็ดสตางค์'
);
expect(convert(('152555.4' as unknown) as number)).toBe(
'หนึ่งแสนห้าหมื่นสองพันห้าร้อยห้าสิบห้าบาทสี่สิบสตางค์'
);
expect((convert('535.') as unknown) as number).toBe(
'ห้าร้อยสามสิบห้าบาทถ้วน'
);
});

it('should convert with leading zero before the decimal point in string format.', () => {
Expand Down Expand Up @@ -226,6 +254,46 @@ describe('convert', () => {
}
});

it('IEEE 754 Case String', () => {
expect(convert(('283798.29' as unknown) as number)).toBe(
'สองแสนแปดหมื่นสามพันเจ็ดร้อยเก้าสิบแปดบาทยี่สิบเก้าสตางค์'
);

expect(convert(('486293.57' as unknown) as number)).toBe(
'สี่แสนแปดหมื่นหกพันสองร้อยเก้าสิบสามบาทห้าสิบเจ็ดสตางค์'
);

expect(convert(('552164.58' as unknown) as number)).toBe(
'ห้าแสนห้าหมื่นสองพันหนึ่งร้อยหกสิบสี่บาทห้าสิบแปดสตางค์'
);
});

it('IEEE 754 Case Small Number (<1000)', () => {
expect(convert(0.29)).toBe('ยี่สิบเก้าสตางค์');

expect(convert(553.57)).toBe('ห้าร้อยห้าสิบสามบาทห้าสิบเจ็ดสตางค์');

expect(convert(790.58)).toBe('เจ็ดร้อยเก้าสิบบาทห้าสิบแปดสตางค์');
});

it('IEEE 754 Case Big Number (>100000)', () => {
expect(convert(283798.29)).toBe(
'สองแสนแปดหมื่นสามพันเจ็ดร้อยเก้าสิบแปดบาทยี่สิบเก้าสตางค์'
);

expect(convert(486293.57)).toBe(
'สี่แสนแปดหมื่นหกพันสองร้อยเก้าสิบสามบาทห้าสิบเจ็ดสตางค์'
);

expect(convert(874552164.58)).toBe(
'แปดร้อยเจ็ดสิบสี่ล้านห้าแสนห้าหมื่นสองพันหนึ่งร้อยหกสิบสี่บาทห้าสิบแปดสตางค์'
);

expect(convert(5143289600432.29)).toBe(
'ห้าล้านหนึ่งแสนสี่หมื่นสามพันสองร้อยแปดสิบเก้าล้านหกแสนสี่ร้อยสามสิบสองบาทยี่สิบเก้าสตางค์'
);
});

// it('equals to value from other library (STRESS TEST)', () => {
// for (let i = 1; i < 20000000; i += 1) {
// expect(convert(i)).toEqual(bahtLatest(i));
Expand Down

1 comment on commit dc1b1f8

@vercel
Copy link

@vercel vercel bot commented on dc1b1f8 Mar 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.