<p>I have a set of floats with varying format and I want to get them all to a common format. </p>
<p>The data is pricing data and comes in the various formats listed below.
- 21
- 21.7
- 21.65</p>
<p>and I want to convert these to integers based on <a href="http://www.martinfowler.com/eaaCatalog/money.html">Martin Fowlers Money</a> representation. </p>
<p>The data comes in via json and I need to then convert it into the correct int format. For example for the 21 it needs to be converted to 2100 to represent £21.00, 21.7 needs to become 2170 and finally 21.65 to 2165.</p>
<p>Does anyone have any advise on best to achieve this? </p>
<p>Should I parse the data out of JSON to a float64? Then how should I approach the conversion?</p>
<p>I tried to convert from float64 to string so that it would have the prevision I needed but that didn't work as I expected. </p>
<p>I was thinking that I need some way of figuring out the format of the data and then doing a specific conversion on it based on the format.</p>
<p>Edit: I think I haven't been clear as to my problem, but am struggling on finding the right APIs that will allow me to do what I want. Would if for example be easier to always work with strings first then finally convert back to a int when I have normalised the data to how I need it? Or should I parse the data from JSON to float64 then work on the conversion? </p>
<hr/>**评论:**<br/><br/>scanner88: <pre><p>Are you unmarshaling the JSON as a float64? If you are, why not just multiply the value by 100 and convert that to an int? If it's unmarshaled as a string, you just need to call strconv.ParseFloat to convert it to a float64 (or preferably update your struct to do the conversion for you) and then do the multiplication and int conversion.</p></pre>villiger2: <pre><p>Seems like some simple string manipulation, if there is no decimals, add two zeros to the string, if there is one decimal place add one zero, if there is two don't add any, then finally remove the period. based on your sample input that should suffice? then you can safely convert from string to Int.</p>
<p>also, a question of this level is probably better suited to <a href="/r/learnprogramming" rel="nofollow">/r/learnprogramming</a> </p></pre>aranw: <pre><p>I know that is what I need, I should of phrased the question more towards how can I do that in Go? </p>
<p>Edit: For example because of this problem would be better to always deal with strings until I've got it into the correct format and then convert back to int? Or should I always start off with a float64 type? </p></pre>szabba: <pre><p>You shouldn't use floating point numbers for representing money.</p></pre>semi-: <pre><p>Just incase it isn't clear why:</p>
<p>$0.10 + $0.20 = $0.30</p>
<p><a href="http://play.golang.org/p/oK26vqS8ah">0.10 + 0.20 = 0.30000000000000004</a></p></pre>gohacker: <pre><p>JavaScript has only floats, though. No integers.</p></pre>szabba: <pre><p>They're not using JavaScript though, they're reading from JSON. They can do exact math on what they reads from JSON if they do things carefully.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传