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!
Oldest comments (6)
In Go!
Go Playground
My solution in js
Elm (point-free style)
Test it online
Here.
c#:
Some comments may only be visible to logged-in visitors. Sign in to view all comments.