Skip to content

Have you ever needed to truncate a number and didn't found any JS native method that just remove the wanted decimal places? Yeah, i had it.

License

Notifications You must be signed in to change notification settings

Nethanos/to-fixed-for-real

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

to-fixed-for-real

npm version License: MIT

A robust JavaScript utility to truncate numbers to a fixed number of decimal places—without rounding errors. Ideal for currency and financial calculations where precision and predictability are critical.


✨ Features

  • Truncates numbers to a specified number of decimal places (no rounding)
  • Handles both string and number inputs
  • Accepts commas or periods as decimal separators
  • Supports negative numbers
  • Lightweight and dependency-free
  • Fully tested with Jest

Motivation

JavaScript’s native toFixed method rounds values, which can lead to inaccuracies in financial or scientific applications. This package provides a reliable way to truncate (not round) numbers to the desired decimal places, supporting both string and number inputs, and handling commas as decimal separators.


Installation

npm install to-fixed-for-real

Usage

Basic Example

const toFixed = require('to-fixed-for-real');

console.log(toFixed(12.599, 2)); // 12.59
console.log(toFixed('982,51832', 2)); // 982.51
console.log(toFixed(10, 2)); // 10.0
console.log(toFixed(-21381923.32938989, 2)); // -21381923.32

Input/Output Table

Input Code Example Output
12.599, 2 toFixed(12.599, 2) 12.59
'982,51832', 2 toFixed('982,51832', 2) 982.51
10, 2 toFixed(10, 2) 10.0
-4.3, 2 toFixed(-4.3, 2) -4.3
1.2345, 2 toFixed(1.2345, 2) 1.23
'10.58928', 2 toFixed('10.58928', 2) 10.58
'164252,', 2 toFixed('164252,', 2) 164252.0
0, 2 toFixed(0, 2) 0

Edge Cases

// Handles string input with comma as decimal
console.log(toFixed('164252,', 2)); // 164252.0

// Handles string input with no decimals
console.log(toFixed('10', 2)); // 10.0

// Handles zero
console.log(toFixed(0, 2)); // 0

API

toFixed(num, floatingPoint)

Parameter Type Description
num number | string The value to truncate. Accepts numbers or strings (with . or ,).
floatingPoint number The number of decimal places to keep. If omitted, all decimals are kept.

Returns: number — The truncated value.

Behavior

  • Truncates, does not round.
  • Handles negative numbers and string inputs.
  • Converts commas to periods for decimal separation.

Testing

This project uses Jest.

npm test

Contributing

Contributions, issues, and feature requests are welcome! Please:

  • Open an issue for bugs or feature requests
  • Fork the repo and submit a pull request for improvements
  • Add tests for new features or edge cases

License

This project is licensed under the MIT License.


Author

nethanos

About

Have you ever needed to truncate a number and didn't found any JS native method that just remove the wanted decimal places? Yeah, i had it.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published