Fork me on GitHub
#beginners
<
2015-11-03
>
roberto17:11:33

Is there a way to convert a string like “$10.00” to its numeric value?

roberto17:11:46

other than parsing the string and removing the $

surreal.analysis18:11:48

DecimalFormat in java is probably what you want to use, if you want to relegate the parsing to something else

surreal.analysis18:11:56

Sorry, NumberFormat

surreal.analysis18:11:02

@roberto: This should work

(let [currencyParser (java.text.NumberFormat/getCurrencyInstance java.util.Locale/US)] 
  (.parse currencyParser "$10.00"))
;; => 10

roberto18:11:29

it fails with something like -$10

roberto18:11:58

I resorted to replace 😞

(defn str->bigdec
  [v]
  (-> (clojure.string/replace v #"\$" "")
      (clojure.string/replace #"," "")
      bigdec))

surreal.analysis18:11:20

For future reference, you probably just needed to change the negative prefix / suffix

surreal.analysis18:11:21

user> (def currencyParser (java.text.NumberFormat/getCurrencyInstance java.util.Locale/US))
;; => #'user/currencyParser
user> (.getNegativePrefix  currencyParser)
;; => "($"
user> (.setNegativePrefix currencyParser "-$")
;; => nil
user> (.setNegativeSuffix currencyParser "")
;; => nil
user> (.parse currencyParser "-$10")
;; => -10
user> 

roberto18:11:12

ugh! Why didn’t they override the parse function so you can pass in the prefix? Hmm, I wonder if the negative prefix would ever be something other than - ????

wamaral18:11:39

Financial systems use the ($10.00) standard for negative currency

wamaral18:11:58

accountants, bankers, etc

roberto18:11:08

then this bank sucks, because they are using -

wamaral18:11:21

I mean, it would probably be easier to display like that for end-users

roberto18:11:24

or maybe they just did that to comply with intuit’s format

roberto18:11:36

I’m working with the raw data they provided

roberto18:11:54

so I would assume they wouldn’t be worried about presentation.

wamaral18:11:28

Then I have no idea simple_smile

roberto18:11:54

sorry for sounding so whiney, working with obscure data formats (ofx) has me in a sour mood.

ajmagnifico19:11:05

Can anyone tell me if it is bad practice to directly access fields on an object created from a type?

roberto20:11:37

oh, a type, I was thinking a record.

roberto20:11:16

I would say no, but maybe more experienced ppl have a more informed decision

xlevus21:11:17

What's the deal with code-reloading in Pedestal? I've followed the instructions in https://github.com/pedestal/pedestal/issues/290 Which works, but as soon as the view is in a different namespace from the routes, you've gotta reload both the views and the routes... Which is pretty annoying.

twillis22:11:08

I dont have any experience with pedestal but could it be wrapped in ring's reload middleware? https://github.com/ring-clojure/ring/blob/master/ring-devel/src/ring/middleware/reload.clj

twillis22:11:49

it seems to have something like the ns tracker that is mentioned in the issue

xlevus22:11:34

will probably do that