Menu
Codebook LogoCodebookDocsTemplates
Design SystemsJavaScriptReactPHP
Codebook LogoCodebook
  • Docs
  • Templates
  • Design Systems
  • JavaScript
  • React
  • PHP
  • Getting Started
    • Getting Started
  • JavaScript
    • React
      • Basics
      • Context
      • Forms
      • Hooks
      • Snippets
      • Examples
        • Hide on Scroll
        • Hide Sidebar on Mobile
        • Sync State Between Tabs
      • Gatsby
        • Config
      • NextJS
        • MDX
        • Router
        • Tips
      • Styled Components
        • Basics
        • Snippets
        • Typescript
    • React Native
      • Introduction
      • Basics
      • Design Systems
      • 3rd Party Components
      • React Native Web
    • NodeJS
      • Basics
      • Async
      • package.json
      • ExpressJS
        • Error Handling
      • NestJS
        • Introduction
        • Basics
    • Typescript
      • Config
    • Vanilla JS
      • Currency
      • Validation
    • Web Components
      • Storybook
      • Integrating with ReactJS
    • jQuery
      • Best Practices
  • PHP
    • Basics
    • Snippets
    • Laravel
      • Blade
      • DevOps
      • Tips
  • CSS
    • Snippets
  • Dev Ops
    • Project Management
      • Linting
    • Git
      • Snippets
    • Ubuntu
      • Permissions
  • Other
    • Regex
    • Shopify
    • Netlify
    • Firebase
      • Introduction
      • Basics
      • Gatsby
  • Design Systems
    • Theming
      • Examples

Currency in JavaScript

Parsing money integers#

Round integer to 2 digits (for money/currency) - via: https://stackoverflow.com/a/12830454 -- https://stackoverflow.com/a/11665949

If the value is a text type:

parseFloat("123.456").toFixed(2);

If the value is a number:

var numb = 123.23454;
numb = numb.toFixed(2);

If the number has a comma:

parseFloat(yournumber.replace(/,/g, ""));
// $1,933.00 becomes 1933
Previous
« Typescript Configuration
Next
Validation »
Ryosuke logo
Copyright © 2020 Ryosuke