-
-
Save risardi123/3ab630630a69e294a63b421060731958 to your computer and use it in GitHub Desktop.
delimiter
This file contains 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 delimiterNumberOnInput = (value, comma) => { | |
let number = value | |
if (number.charAt(0) === ",") { | |
number = "" | |
} else if (parseFloat(number) === 0 && number.charAt(1) !== ",") { | |
number = "0" | |
} else if (number.charAt(0) === "0") { | |
if (!(number.charAt(1) === ",")) { | |
number = number.charAt(1) | |
} | |
} | |
if (number.includes(".")) { | |
number = number.split(".").join("") | |
} | |
if (comma) { | |
number = number.replace(/[^,\d]/g, "") | |
} else { | |
number = number.replace(/[^\d]/g, "") | |
} | |
number = number.replace(",,", ",") | |
let splitNumber = number.split(",") | |
let valueNumber = "" | |
if (splitNumber[1]) { | |
splitNumber = [splitNumber[0], splitNumber[1]] | |
valueNumber = splitNumber[0] | |
.toString() | |
.replace(/\B(?=(\d{3})+(?!\d))/g, ".") | |
valueNumber = `${valueNumber}${ | |
splitNumber[1] ? "," + splitNumber[1] : "" | |
}` | |
} else { | |
valueNumber = number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".") | |
} | |
console.log(valueNumber) | |
return valueNumber | |
} |
This file contains 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 delimiterNumberOnText = (value) => { | |
let number = value | |
if (number) { | |
number = number.toString() | |
if (number) { | |
let splitNumber = number.split(".") | |
let valueNumber = splitNumber[0].replace(/\B(?=(\d{3})+(?!\d))/g, ".") | |
valueNumber = `${valueNumber}${ | |
splitNumber[1] ? "," + splitNumber[1] : "" | |
}` | |
return valueNumber | |
} | |
return value | |
} else { | |
return "0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment