Skip to content
Advertisement

divide number with decimals javascript

how can I divide number (money) to x number equally the number could be with one or two decimal or without it

such as 1000 or 100.2 or 112.34
I want to be able to split that number into x part all of them equally, however if it’s not odd number the extra number to the last one.

for example

JavaScript

Advertisement

Answer

Sounds like a pretty straightforward loop/recursion.

Here you go

JavaScript

To get it to be two decimals of currency multiply both numbers by 100 before calling this function then divide each result by 100 and call toFixed(2).

Like so

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement