packagedollarsimport("testing")vartestCases=[]struct{descriptionstringinputfloat64expectedstring}{{"only cents",.04,"$0.04",},{"only dollars",3,"$3.00",},{"cents & dollars",3.14,"$3.14",},{"amounts with more than two decimal places round to the nearest cent",4.478,"$4.48",},}funcTestDollars(t*testing.T){for_,test:=rangetestCases{ifresult:=Dollars(test.input);result!= test.expected {
t.Fatalf("FAILED: %s - Dollars(%f): %s, expected %s \n",test.description,test.input,result,test.expected)}t.Logf("PASS: %s \n",test.description)}}
Yeah it rounds to the nearest cent in this case, the question becomes then do we want it to work that way, or do we say "Hey you don't have a full cent" so it always rounds down?
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Go - with tests as usual
fun fact - this was the first time I wrote anything using Vim
dollars.go
dollars_test.go
I'm not sure, but I think that "Sprintf" rounds to nearest integer.
Try with a value like 3.149 if still returns 3.14 or 3.15
Added additional test case to clarify current rounding behavior.
Yeah it rounds to the nearest cent in this case, the question becomes then do we want it to work that way, or do we say "Hey you don't have a full cent" so it always rounds down?