Skip to content

Bug Report: unmaskAsNumber with brl-currency mask incorrectly converts decimal values #136

@kurybr

Description

@kurybr

Description

When using withHookFormMask with the brl-currency mask and unmaskAsNumber: true, decimal values like 0.5 are incorrectly converted to 5 in the onBeforeMask callback.

Steps to Reproduce

  1. Set up a form field with withHookFormMask using brl-currency mask
  2. Configure with unmaskAsNumber: true
  3. Add an onBeforeMask callback to log the values
  4. Enter a decimal value like 0.5
  5. Observe the logged values in onBeforeMask

Expected Behavior

  • Input: 0.5onBeforeMask should receive "0.5" → Final value should be 0.5

Actual Behavior

  • Input: 0.5onBeforeMask receives "0.5" initially → Then receives "5" → Final value becomes 5

Code Example

const inputProps = withHookFormMask(register, 'brl-currency', {
  autoUnmask: true,
  removeMaskOnSubmit: true,
  rightAlign: false,
  unmaskAsNumber: true,
  onBeforeMask: (initialValue, opts) => {
    console.log('[withHookFormMask] onBeforeMask', { initialValue, opts });
    // initialValue changes from "0.5" to "5" unexpectedly
    return initialValue;
  }
});

Console Output

[withHookFormMask] onBeforeMask { initialValue: '0.5', opts: {...} }
[withHookFormMask] onBeforeMask { initialValue: '5', opts: {...} }
[withHookFormMask] onBeforeMask { initialValue: '5', opts: {...} }

Environment

  • Library Version: use-mask-input (latest)
  • React Version: 19.1.0
  • Node Version: (your node version)
  • Browser: (your browser)

Additional Context

  • This issue specifically affects decimal values when using unmaskAsNumber: true
  • The problem occurs with the brl-currency mask (Brazilian Real format)
  • Values like 0.5, 0.25, 0.75 all get converted incorrectly
  • The issue doesn't occur when unmaskAsNumber: false

Workaround

Currently using unmaskAsNumber: false and manually converting the string value to number when needed, but this defeats the purpose of the unmaskAsNumber option.

Impact

This bug makes the library unusable for currency inputs that need to handle decimal values correctly, which is a common use case for financial applications.

Additional Information

  • The bug seems to be related to how the brl-currency mask processes decimal separators
  • The issue occurs during the masking process, not in the final output
  • This affects Brazilian Real currency formatting where comma (,) is used as decimal separator

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions