<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[solved] Addition of Float does not give precise result]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I am doing addition of prices in my application. I have values as QStrings and I convert to float and add. When it reaches high value, result is not precise. I read like float or double should not be used for currency addition and use separate variable for integer part and fractional part. My code and log is below:</p>
<p dir="auto">@    qDebug ("m_totalPrice: %f + %f =", m_totalPrice, item.price.toFloat());<br />
m_totalPrice += item.price.toFloat();<br />
qDebug ("m_totalPrice: %f", m_totalPrice);<br />
ui-&gt;totalLabel-&gt;setText(QString::number(m_totalPrice, 'f', 2));<br />
@</p>
<p dir="auto">m_totalPrice is float and item.price is QString</p>
<p dir="auto">logs:</p>
<h1>m_totalPrice: 424659.500000 + 22350.500000 =</h1>
<p dir="auto">m_totalPrice: 447010.000000</p>
<h1>m_totalPrice: 447010.000000 + 18.900000 =</h1>
<p dir="auto">m_totalPrice: 447028.906250</p>
<h1>m_totalPrice: 447028.906250 + 125.900002 =</h1>
<p dir="auto">m_totalPrice: 447154.812500</p>
<h1>m_totalPrice: 447154.812500 + 125.900002 =</h1>
<p dir="auto">m_totalPrice: 447280.718750</p>
<h1>m_totalPrice: 447280.718750 + 11.500000 =</h1>
<p dir="auto">m_totalPrice: 447292.218750</p>
<h1>m_totalPrice: 447292.218750 + 22.500000 =</h1>
<p dir="auto">m_totalPrice: 447314.718750</p>
<p dir="auto">Error is from line 2. Can someone tell how to resolve this?</p>
]]></description><link>https://forum.qt.io/topic/42943/solved-addition-of-float-does-not-give-precise-result</link><generator>RSS for Node</generator><lastBuildDate>Sat, 02 May 2026 05:52:15 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/42943.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 25 Jun 2014 08:43:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [solved] Addition of Float does not give precise result on Sat, 28 Jun 2014 09:48:30 GMT]]></title><description><![CDATA[<p dir="auto">[quote author="Abin" date="1403934198"]Thanks all,</p>
<p dir="auto">using double solved the issue[/quote]</p>
<p dir="auto">Not quite. The issue is still there, hiding from you. As your numbers cannot be represented exactly as base-2 floating point numbers, you will always have rounding errors, and eventually that may still affect your computation. The correct solution is to use integer arithmetic on cents.</p>
]]></description><link>https://forum.qt.io/post/233804</link><guid isPermaLink="true">https://forum.qt.io/post/233804</guid><dc:creator><![CDATA[mmoll]]></dc:creator><pubDate>Sat, 28 Jun 2014 09:48:30 GMT</pubDate></item><item><title><![CDATA[Reply to [solved] Addition of Float does not give precise result on Sat, 28 Jun 2014 05:43:18 GMT]]></title><description><![CDATA[<p dir="auto">Thanks all,</p>
<p dir="auto">using double solved the issue</p>
]]></description><link>https://forum.qt.io/post/233793</link><guid isPermaLink="true">https://forum.qt.io/post/233793</guid><dc:creator><![CDATA[Abin]]></dc:creator><pubDate>Sat, 28 Jun 2014 05:43:18 GMT</pubDate></item><item><title><![CDATA[Reply to [solved] Addition of Float does not give precise result on Thu, 26 Jun 2014 22:30:16 GMT]]></title><description><![CDATA[<p dir="auto"><a href="http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html</a></p>
]]></description><link>https://forum.qt.io/post/233636</link><guid isPermaLink="true">https://forum.qt.io/post/233636</guid><dc:creator><![CDATA[mmoll]]></dc:creator><pubDate>Thu, 26 Jun 2014 22:30:16 GMT</pubDate></item><item><title><![CDATA[Reply to [solved] Addition of Float does not give precise result on Thu, 26 Jun 2014 21:20:57 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto"><em>float</em> is a 32bit data type with 8 precision digits; you need more precision so you must use <em>double</em> (16 precision digits) or <em>long double</em> (24 precision digits).</p>
<p dir="auto"><em>PS</em>: long double precision depends on the platform</p>
]]></description><link>https://forum.qt.io/post/233617</link><guid isPermaLink="true">https://forum.qt.io/post/233617</guid><dc:creator><![CDATA[mcosta]]></dc:creator><pubDate>Thu, 26 Jun 2014 21:20:57 GMT</pubDate></item></channel></rss>