add posts

This commit is contained in:
jetstream0
2023-08-05 14:50:06 -07:00
parent 192331d27f
commit 6e31b888d8
9 changed files with 344 additions and 16 deletions

View File

@@ -20,13 +20,15 @@ I think the most interesting part of this code was the 5th line (`let position =
![Demo](/images/commas.gif)
Here is a version that can handle decimals and invalid inputs:
Here is a version that can handle decimals (keep in mind that Javascript does cut off decimals after a certain point), negative numbers and invalid inputs:
```js
function format_commas(amount) {
if (isNaN(Number(amount))) {
return amount;
}
let negative = amount < 0;
amount = Math.abs(amount);
let before_dec = String(amount).split('.')[0];
let amount_mod = before_dec;
//iterate the amount of commas there are
@@ -37,6 +39,9 @@ function format_commas(amount) {
if (String(amount).split('.')[1]) {
amount_mod = amount_mod+"."+String(amount).split('.')[1];
}
if (negative) {
amount_mod = `-${amount_mod}`;
}
return amount_mod;
}
```