Last active
July 23, 2024 19:43
-
-
Save DJj123dj/39a4daf6cb68d4d14be20466a64ce2a7 to your computer and use it in GitHub Desktop.
Advanced string to number converter!
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
/** | |
===DESCRIPTION=== | |
This is a function that converts all sorts of string numbers to the Number type. | |
It can detect the decimal point automatically, so you don't have to worry about multiple points in your number! | |
Notice: | |
This is not meant to be used for normal text like "hello, how is it going?", | |
It's probably going to crash if you do so. | |
======USAGE====== | |
Input: String | |
Output: Number | |
Input examples: | |
123.345.234,2 => 123345234.2 | |
485,342,56.14 => 48534256.14 | |
1000,35 => 1000.35 | |
2467.467 => 2467.467 | |
*/ | |
const stringToNumberConverter = (input) => { | |
/**@type {[{type:"number"|"point", value:String}]} */ | |
const firstArray = [] | |
/** @type {false|"."|","}*/ | |
var detectedSplitPoint = false | |
/** @type {false|"."|","}*/ | |
var detectedDecimalPoint = false | |
var amountOfPoints = 0 | |
const pointArray = [] | |
//first cycle | |
input.split("").forEach((n) => { | |
if (["0","1","2","3","4","5","6","7","8","9"].includes(n)){ | |
firstArray.push({ | |
type:"number", | |
value:n | |
}) | |
}else if (n == "." || n == ","){ | |
firstArray.push({ | |
type:"point", | |
value:n | |
}) | |
amountOfPoints++ | |
pointArray.push(n) | |
} | |
}) | |
/**@type {[{type:"number"|"splitpoint"|"decimalpoint", value:String}]} */ | |
const secondArray = [] | |
//second cycle | |
if (amountOfPoints == 0){ | |
firstArray.forEach((n) => { | |
if (n.type == "number") return secondArray.push({type:"number",value:n.value}) | |
}) | |
}else if (amountOfPoints == 1){ | |
firstArray.forEach((n) => { | |
if (n.type == "number") return secondArray.push({type:"number",value:n.value}) | |
if (n.type == "point"){ | |
detectedDecimalPoint = n.value | |
secondArray.push({type:"decimalpoint",value:n}) | |
} | |
}) | |
}else if (amountOfPoints > 1){ | |
if (pointArray[0] != pointArray[pointArray.length-1]){ | |
detectedDecimalPoint = pointArray[pointArray.length-1] | |
detectedSplitPoint = pointArray[0] | |
}else { | |
detectedSplitPoint = pointArray[0] | |
} | |
firstArray.forEach((n) => { | |
if (n.type == "number") return secondArray.push({type:"number",value:n.value}) | |
if (n.value == detectedSplitPoint) return secondArray.push({type:"splitpoint",value:n.value}) | |
if (n.value == detectedDecimalPoint) return secondArray.push({type:"decimalpoint",value:n.value}) | |
}) | |
} | |
var numberString = "" | |
secondArray.forEach((n) => { | |
if (n.type == "number"){ | |
numberString = numberString+n.value | |
}else if (n.type == "splitpoint"){ | |
//do nothing with split points | |
return | |
}else if (n.type == "decimalpoint"){ | |
numberString = numberString+"." | |
} | |
}) | |
return Number(numberString) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment