DEV Community

dev.to staff
dev.to staff

Posted on

2 2

Daily Challenge #85 - Unwanted Dollars

If you are faced with an input box like this:

                                           +--------------+
  Enter the price of the item, in dollars: |              |
                                           +--------------+

Do you put the dollar sign in or not? Inevitably, some people will type a dollar sign while others will leave it out. The instructions could be made clearer - but that won't stop those that don't read them.

Write a function that converts a string representing a number into the number itself. Consider negative numbers and any extraneous space characters that the user might put in. If the given string does not represent a number, return 0.0.

Sample Cases:
money_value("12.34")
money_value(" $5.67")
money_value("-$ 0.1")
money_value("$-2.3456")
money_value("007")
money_value(" $ 89")
money_value("   .11")
money_value("$.2")
money_value("-.34")
money_value("$$$")

This challenge comes from geoffp on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Top comments (6)

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

Collapse
 
aminnairi profile image
Amin

Elm (point-free style)

import String exposing (replace, toFloat)
import Maybe exposing (withDefault)

moneyValue : String -> Float
moneyValue  =
    replace "$" ""
        >> replace " " ""
        >> toFloat
        >> withDefault 0.0

Test it online

Here.

Collapse
 
peledzohar profile image
Zohar Peled • Edited

c#:

double money_value(string input)
{
    return double.TryParse(input.Replace("$", "").Replace(" ", ""), out double result) ? 
        result : 
        0.0;
}
Collapse
 
kvharish profile image
K.V.Harish

My solution in js

const moneyValue = (str) => isNaN(a = parseFloat(str.replace(/[^0-9.]/g, ''))) ? 0 : a;
Collapse
 
lormayna profile image
lormayna • Edited
import unittest
def money_value(s):
r = str(s).replace("$", "").replace(" ", "")
try:
return float(r)
except:
return 0.0
class CleanTest(unittest.TestCase):
def test(self):
self.assertEqual(money_value(0), 0.0)
self.assertEqual(money_value("12.34"), 12.34)
self.assertEqual(money_value(" $5.67"), 5.67)
self.assertEqual(money_value("-$ 0.1"), -0.1)
self.assertEqual(money_value("$-2.3456"), -2.3456)
self.assertEqual(money_value("007"), 7)
self.assertEqual(money_value(" $ 89"), 89)
self.assertEqual(money_value(" .11"), 0.11)
self.assertEqual(money_value("$.2"), 0.2)
self.assertEqual(money_value("-.34"), -0.34)
self.assertEqual(money_value("$$$"), 0.0)
if __name__ == "__main__":
c=CleanTest()
c.test()
view raw challenge85.py hosted with ❤ by GitHub

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay