DEV Community

Discussion on: Daily Challenge #276 - Unwanted Dollar Signs

Collapse
 
bubbler4 profile image
Bubbler

APL (using Dyalog APL):

      MoneyValue←{⍎⍵~'$ '}  ⍝ ⍵: dollar string
      MoneyValue '12.34'
12.34
      MoneyValue '-0.89'
¯0.89
      MoneyValue '.11'
0.11
      MoneyValue '007'
7
      MoneyValue '-$ 0.1'
¯0.1
      MoneyValue '$-2.3456'
¯2.3456
      MoneyValue '$.2'
0.2

(Use the bookmarklet on this post to see the code with APL font and syntax highlighting.)

Removes the characters $ and space using "set difference" ~, and evaluates the resulting string as APL expression. The preceding - gets evaluated as the "negate" function, and APL's numeric notation uses ¯ (read "high minus") for the negative sign.