Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 7, 2025

Description

Implements FORMAT(number, decimals[, locale]) function combining MySQL-style numeric formatting capabilities. Formats numbers with comma-separated thousands and configurable decimal precision.

Changes

Function Implementation (src/55functions.js)

  • Added concise stdfn.FORMAT runtime function (13 lines)
  • Handles null/undefined, NaN, negative numbers, string inputs
  • Uses toFixed() for rounding to avoid floating-point precision issues
  • Regex-based thousands separator insertion
  • Uses destructuring for cleaner code

Test Coverage (test/test105-B.js)

  • 8 test cases: basic formatting, negatives, nulls, table queries, precision levels, edge cases

Usage

alasql('SELECT FORMAT(1234567.89, 2)')  // "1,234,567.89"
alasql('SELECT FORMAT(-999.99, 0)')     // "-1,000"
alasql('SELECT FORMAT(price, 2) FROM products')

// In table context
alasql('CREATE TABLE prices (id INT, price DECIMAL(10,2))')
alasql('INSERT INTO prices VALUES (1, 1234.567)')
alasql('SELECT id, FORMAT(price, 2) AS formatted FROM prices')
// [{id: 1, formatted: "1,234.57"}]

Notes

Third parameter locale is reserved for future T-SQL/Oracle/PostgreSQL format string support per original issue request. Implementation aligns with MySQL's FORMAT() reference specification.

Original prompt

This section details on the original issue you should resolve

<issue_title>Add FORMAT function</issue_title>
<issue_description>I think about combination of these functions

Plus there are some other Oracle and Postres formatting functions.
</issue_description>

<agent_instructions>Name the test file test/test105-B.js

</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@mathiasrw Added to requests so people can vote for it

http://feathub.com/agershun/alasql/+26


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Dec 7, 2025
Copilot AI and others added 3 commits December 7, 2025 06:20
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Copilot AI changed the title [WIP] Add FORMAT function for multiple SQL databases Add FORMAT function for numeric formatting with thousands separators Dec 7, 2025
Copilot AI requested a review from mathiasrw December 7, 2025 06:28
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add FORMAT function

2 participants