(Medium) - How To Build A Money Data Type In JavaScript
This article is too short to generate a summary.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| const toDecimalPoint = (cents) => { | |
| const sign = cents < 0 ? '-' : ''; | |
| const columns = cents < 0 | |
| ? cents.toString().split('').slice(1, Infinity) | |
| : cents.toString().split(''); | |
| const result = columns.slice(); | |
| const dotLocation = columns.length - 2; | |
| if (dotLocation < 0) { | |
| return sign + '$0.0' + result.join(''); | |
| } | |
| if (dotLocation === 0) { | |
| return sign + '$0.' + result.join(''); | |
| } | |
| if (dotLocation > 0) { | |
| result.splice(dotLocation, 0, '.'); | |
| return sign + '$' + result.join(''); | |
| } | |
| }; |
Report this article
Tell us what's wrong. Every field is optional.