DEV Community

Discussion on: Daily Challenge #85 - Unwanted Dollars

Collapse
 
pmkroeker profile image
Peter

In Go!

func moneyValue(value string) float64 {
    // check if negative is in the incoming string, in cases of -$12.23
    isNegative := strings.Contains(value, "-")
    val, err := strconv.ParseFloat(value, 64)
    for err != nil && len(value) > 0 {
        value = value[1:]
        val, err = strconv.ParseFloat(value, 64)
    }
    // catch for leading '-' placements, in cases of -$12.23
    if isNegative && val > 0 {
        val = -val
    }
    return val
}

Go Playground