๐ŸŽจ jQuery Calx - Formatters Example

About Formatters: This example demonstrates the built-in formatters (currency, percent, number, integer, text), input-mask behavior, and conditional styling with smart parsing.

Basic Formatters

๐Ÿ’ฐ Currency Formatter

Try typing: 5000, $500, 123.45, or $1234
Click to see raw value, blur to see formatted

๐Ÿ“Š Percent Formatter

Try typing: 15 (becomes 15.00%), 15%, or 0.15
Multiplies by 100 and adds %

๐Ÿ”ข Number Formatter

Try: 99.999 or 3.14159
Rounds to 2 decimal places

๐ŸŽฏ Integer Formatter

Try: 50.5 or 99.4
Rounds to whole number

๐Ÿ“ Text Formatter

Try: text, numbers, or mixed
Converts to string

Real-World Example: Shopping Cart

๐Ÿ›’ Product 1

๐Ÿ›’ Product 2

๐Ÿ’ณ Totals

Conditional Styling Example: Profit/Loss

๐Ÿ“Š Month 1

Negative values show in red

๐Ÿ“Š Month 2

Try changing revenue to see red/black colors

๐Ÿ’ฐ Total

Red = Loss, Black = Profit

Financial Example: Loan Calculator

๐Ÿ’ฐ Loan Details

๐Ÿ“ˆ Monthly Payment

Annual rate รท 12
Years ร— 12
Using PMT function

๐Ÿ’ต Total Cost

Monthly payment ร— number of payments
Total paid - principal

๐Ÿ’ก How to Use

๐Ÿ“š Code Example

Here's how to use formatters with input-mask behavior:

<!-- Currency input with mask - accepts 2, $2, $2.00 -->
<input data-cell="A1" value="1234.56" data-format="currency">

<!-- Named variable using data-var -->
<input data-cell="B1" data-var="TAX" value="0.08" data-format="percent">

<!-- Use the named variable in formulas -->
<input data-formula="A1*TAX" data-format="currency" readonly>

<!-- Percent input with mask - accepts 15, 15%, 0.15 -->
<input data-cell="A2" value="0.08" data-format="percent">

<!-- Readonly calculated field with formatting -->
<input data-formula="A1*1.08" data-format="currency" readonly>

<!-- Display-only element -->
<div data-formula="A1*A2" data-format="currency"></div>

<!-- Initialize Calx -->
<script>
$('#myform').calx({
    autoCalculate: true
});
</script>

๐ŸŽจ Available Formatters

Input-mask feature: All formatters intelligently parse your input, so you can type naturally!

Custom formatters: You can register your own formatters with parse() and format() methods!

๐ŸŽจ Conditional Styling (Style Formatters)

Apply CSS styles based on cell values using data-style-if attribute:

Example:

<!-- Show negative profit in red -->
<input data-formula="REVENUE-EXPENSES"
       data-format="currency"
       data-style-if="negative"
       readonly>

Custom style formatters: Register your own via JavaScript!

$('#myform').calx({
    styleFormatters: {
        warning: function(value, $element) {
            if (value > 1000) {
                return {
                    color: 'orange',
                    fontWeight: 'bold'
                };
            }
            return {};
        }
    }
});