-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c92709
commit 801aecc
Showing
3 changed files
with
49 additions
and
37 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// Smartly parses a string as a float using only the numeric components. | ||
func parseAsFloat(value string, fallback float64) (float64, error) { | ||
numericValue := regexp.MustCompile("[^0-9.]+").ReplaceAllString(value, "") // Strip any non-numeric characters (except for the decimal point) | ||
numericValue = regexp.MustCompile(`\.{2,}`).ReplaceAllString(numericValue, ".") // Collapse multiple decimal points into one | ||
numericValue = strings.Trim(numericValue, ".") // Trim any leading/trailing decimal points | ||
numericValue = strings.TrimSpace(numericValue) // Trim any whitespace | ||
|
||
if numericValue == "" { return fallback, nil } | ||
|
||
return strconv.ParseFloat(numericValue, 64) | ||
} |