-
Notifications
You must be signed in to change notification settings - Fork 689
Add FORMAT function for numeric formatting with thousands separators #2327
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
Draft
Copilot
wants to merge
5
commits into
develop
Choose a base branch
from
copilot/add-format-function
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7ac9fe5
Initial plan
Copilot 064b5ed
Implement FORMAT function with MySQL-style numeric formatting
Copilot f035d68
Address code review feedback: simplify rounding logic and fix code style
Copilot 7fe3a16
Use const for test variable to follow modern JavaScript practices
Copilot b60a612
Refactor FORMAT function to be more concise using destructuring
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,100 @@ | ||
| if (typeof exports === 'object') { | ||
| var assert = require('assert'); | ||
| var alasql = require('..'); | ||
| } | ||
|
|
||
| describe('Test 105-B - FORMAT function', function () { | ||
| const test = '105B'; | ||
|
|
||
| before(function () { | ||
| alasql('create database test' + test); | ||
| alasql('use test' + test); | ||
| }); | ||
|
|
||
| after(function () { | ||
| alasql('drop database test' + test); | ||
| }); | ||
|
|
||
| it('A) MySQL-style numeric formatting - FORMAT(number, decimals)', function () { | ||
| // Basic number formatting with 2 decimal places | ||
| var res = alasql('SELECT VALUE FORMAT(12332.123456, 2)'); | ||
| assert.equal(res, '12,332.12'); | ||
|
|
||
| // Single decimal place | ||
| res = alasql('SELECT VALUE FORMAT(12332.2, 1)'); | ||
| assert.equal(res, '12,332.2'); | ||
|
|
||
| // No decimal places | ||
| res = alasql('SELECT VALUE FORMAT(12332.2, 0)'); | ||
| assert.equal(res, '12,332'); | ||
|
|
||
| // Small number | ||
| res = alasql('SELECT VALUE FORMAT(123.456, 2)'); | ||
| assert.equal(res, '123.46'); | ||
| }); | ||
|
|
||
| it('B) FORMAT with negative numbers', function () { | ||
| var res = alasql('SELECT VALUE FORMAT(-12332.123456, 2)'); | ||
| assert.equal(res, '-12,332.12'); | ||
|
|
||
| res = alasql('SELECT VALUE FORMAT(-999.99, 0)'); | ||
| assert.equal(res, '-1,000'); | ||
| }); | ||
|
|
||
| it('C) FORMAT with zero and NULL', function () { | ||
| var res = alasql('SELECT VALUE FORMAT(0, 2)'); | ||
| assert.equal(res, '0.00'); | ||
|
|
||
| res = alasql('SELECT VALUE FORMAT(NULL, 2)'); | ||
| assert.equal(res, null); | ||
| }); | ||
|
|
||
| it('D) FORMAT in table queries', function () { | ||
| alasql('CREATE TABLE prices (id INT, price DECIMAL(10,2))'); | ||
| alasql('INSERT INTO prices VALUES (1, 1234.567), (2, 98765.4321), (3, 0.99)'); | ||
|
|
||
| var res = alasql('SELECT id, FORMAT(price, 2) AS formatted FROM prices ORDER BY id'); | ||
| assert.deepEqual(res, [ | ||
| {id: 1, formatted: '1,234.57'}, | ||
| {id: 2, formatted: '98,765.43'}, | ||
| {id: 3, formatted: '0.99'}, | ||
| ]); | ||
|
|
||
| alasql('DROP TABLE prices'); | ||
| }); | ||
|
|
||
| it('E) FORMAT with different decimal precision', function () { | ||
| var res = alasql('SELECT VALUE FORMAT(1234.56789, 0)'); | ||
| assert.equal(res, '1,235'); | ||
|
|
||
| res = alasql('SELECT VALUE FORMAT(1234.56789, 1)'); | ||
| assert.equal(res, '1,234.6'); | ||
|
|
||
| res = alasql('SELECT VALUE FORMAT(1234.56789, 3)'); | ||
| assert.equal(res, '1,234.568'); | ||
|
|
||
| res = alasql('SELECT VALUE FORMAT(1234.56789, 4)'); | ||
| assert.equal(res, '1,234.5679'); | ||
| }); | ||
|
|
||
| it('F) FORMAT with very large numbers', function () { | ||
| var res = alasql('SELECT VALUE FORMAT(1234567890.123, 2)'); | ||
| assert.equal(res, '1,234,567,890.12'); | ||
|
|
||
| res = alasql('SELECT VALUE FORMAT(999999999999.99, 2)'); | ||
| assert.equal(res, '999,999,999,999.99'); | ||
| }); | ||
|
|
||
| it('G) FORMAT with numbers less than 1', function () { | ||
| var res = alasql('SELECT VALUE FORMAT(0.123456, 2)'); | ||
| assert.equal(res, '0.12'); | ||
|
|
||
| res = alasql('SELECT VALUE FORMAT(0.999, 0)'); | ||
| assert.equal(res, '1'); | ||
| }); | ||
|
|
||
| it('H) FORMAT with string numbers', function () { | ||
| var res = alasql("SELECT VALUE FORMAT('1234.567', 2)"); | ||
| assert.equal(res, '1,234.57'); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.