Note that there are some explanatory texts on larger screens.

plurals
  1. POCorrectly formatting the output
    primarykey
    data
    text
    <p>My software has to show end result to a user, the data is received though UART port, then I need to format and round the data.</p> <p>I did write algorihtm to do that, but my feeling is that there has to be a better solution.</p> <p>My output can have mutiple ranges, that i need to round and format correctly.</p> <p>Example:</p> <p>1 of many results go from -></p> <p>0.000 Ohm to 2.000 Ohm</p> <p>2.01 Ohm to 20.00 Ohm</p> <p>20.1 Ohm to 100.0 Ohm</p> <p>100 Ohm to 200 Ohm</p> <p>I made a simple structure to hold basic data for formating </p> <pre><code>struct _range { float from; //0.000 for first example float to; //2.000 int decimal; //3, decimal places I need int unit; //Unit to format, Ohm in my example float div; //sometimes I need to divide the result at some range //example, when I reach 1000VA I need to divide by 1000 to get 1kVA }; </code></pre> <p>I have a static function called AddUnitToResult, this one will add unit to my result.</p> <p>Now I am asking you to help me write a function that will correctly format the result to string and round it to double ( I need double for later to compare it ).</p> <p>Correctly format means that even if result is 0, it should format it to 3 dedimal places.</p> <p>I hope you can help me guys</p> <p>Edit:</p> <p>This is what I currently have to handle round and dividing.</p> <pre><code>void ResultBox::SetResult(float res) { this-&gt;measureCounter++; this-&gt;valueAVG +=res; if (res &gt; this-&gt;valueMax) this-&gt;valueMax = res; if (res &lt; this-&gt;valueMin) this-&gt;valueMin = res; float tttmp; if (this-&gt;RangeCount &gt; 0 &amp;&amp; this-&gt;ranges[0].decimal &gt;= 0) { tttmp = Converter::cutDecimal(res,this-&gt;ranges[0].decimal); } int decimal = GetDecimalPlaces(res,0); float div = GetDivision(res); int unit = GetUnit(res); tttmp=tttmp/div; this-&gt;result = tttmp; float tmpRes = res /div; this-&gt;isValid =true; WCHAR resText[20]; WCHAR finalText[20]; WCHAR maxText[20]; WCHAR minText[20]; char resTEXT[20]; tmpRes = res/div;; std::ostringstream ss; ss &lt;&lt; std::fixed &lt;&lt; std::setprecision(decimal) &lt;&lt; tttmp; std::string s = ss.str(); if (decimal &gt; 0 &amp;&amp; s[s.find_last_not_of('0')] == '.') { s.erase(s.size()-decimal+1); } Converter::dtoa(resTEXT,tmpRes); switch(decimal) { case 0: if (floor(tttmp) == tttmp) { swprintf(resText,L"%.0f",tttmp); }else { swprintf(resText,L"%S",s.c_str()); } swprintf(maxText,L"%.0f",this-&gt;GetMax()); swprintf(minText,L"%.0f",this-&gt;GetMin()); break; case 1: if (floor(tttmp) == tttmp) { swprintf(resText,L"%.1f",tttmp); }else { swprintf(resText,L"%S",s.c_str()); } swprintf(maxText,L"%.1f",this-&gt;GetMax()); swprintf(minText,L"%.1f",this-&gt;GetMin()); break; case 2: if (floor(tttmp) == tttmp) { swprintf(resText,L"%.2f",tttmp); }else { swprintf(resText,L"%S",s.c_str()); } swprintf(maxText,L"%.2f",this-&gt;GetMax()); swprintf(minText,L"%.2f",this-&gt;GetMin()); break; case 3: if (floor(tttmp) == tttmp) { swprintf(resText,L"%.3f",tttmp); }else { swprintf(resText,L"%S",s.c_str()); } swprintf(maxText,L"%.3f",this-&gt;GetMax()); swprintf(minText,L"%.3f",this-&gt;GetMin()); break; case 4: if (floor(tttmp) == tttmp) { swprintf(resText,L"%.4f",tttmp); }else { swprintf(resText,L"%S",s.c_str()); } swprintf(maxText,L"%.4f",this-&gt;GetMax()); swprintf(minText,L"%.4f",this-&gt;GetMin()); break; case 5: if (floor(tttmp) == tttmp) { swprintf(resText,L"%.5f",tttmp); }else { swprintf(resText,L"%S",s.c_str()); } swprintf(maxText,L"%.5f",this-&gt;GetMax()); swprintf(minText,L"%.5f",this-&gt;GetMin()); break; } //pogledamo če je majni if (res &lt; this-&gt;GetMin()) { if (LowerEnabled == true) { wcscpy(finalText,L"&lt;"); } else { wcscpy(finalText,L""); } wcscat(finalText,minText); } else if (res &gt; this-&gt;GetMax()) { wcscpy(finalText,L"&gt;"); wcscat(finalText,maxText); } else { wcscpy(finalText,resText); } if (res == this-&gt;GetMin()) { wcscpy(finalText,minText); } if (res == this-&gt;GetMax()) { wcscpy(finalText,maxText); } if (this-&gt;unitBox) { WCHAR mm[10]; wcscpy(mm,L""); TABSGuiProxy::MargeResultAndUnit(mm,unit); if (mm[0] == ' ') this-&gt;unitBox-&gt;SetText(&amp;mm[1]); else this-&gt;unitBox-&gt;SetText(mm); } this-&gt;m_ptextBlock-&gt;SetText(finalText); if (this-&gt;resultLimit) { if (unit == MEASRUEMENT_UNITS::kVA) { float fff = this-&gt;resultLimit-&gt;GetValue()*1000; std::wostringstream ss1; ss1 &lt;&lt; std::fixed &lt;&lt; std::setprecision(decimal) &lt;&lt; fff; std::wstring s1 = ss1.str(); if (decimal &gt; 0 &amp;&amp; s1[s1.find_last_not_of('0')] == '.') { s1.erase(s1.size()-decimal+1); } if (wcscmp(resText,s1.c_str()) == 0) { this-&gt;SetGoodResult(); }else { float mmm = fabs(_wtof(s1.c_str()) - this-&gt;resultLimit-&gt;GetValue()*1000) ; //else if (fabs(IDelResoult - IDeltaLim) &lt;= 0.001 || IDelResoult &lt; IDeltaLim) if (mmm&lt;= 0.001 || tttmp &lt; (_wtof(s1.c_str()))) { this-&gt;SetGoodResult(); } else { this-&gt;SetBadResult(); } } } else { float fff = this-&gt;resultLimit-&gt;GetValue(); std::wostringstream ss1; ss1 &lt;&lt; std::fixed &lt;&lt; std::setprecision(decimal) &lt;&lt; fff; std::wstring s1 = ss1.str(); if (decimal &gt; 0 &amp;&amp; s1[s1.find_last_not_of('0')] == '.') { s1.erase(s1.size()-decimal+1); } if (wcscmp(resText,s1.c_str()) == 0) { this-&gt;SetGoodResult(); } else { float mmm = fabs(_wtof(resText) - this-&gt;resultLimit-&gt;GetValue()) ; //else if (fabs(IDelResoult - IDeltaLim) &lt;= 0.001 || IDelResoult &lt; IDeltaLim) if (mmm&lt;= 0.001 || tttmp &lt; (_wtof(s1.c_str()))) { this-&gt;SetGoodResult(); } else { this-&gt;SetBadResult(); } } } } } </code></pre> <p>Let me try to explain a bit more:</p> <p>I use struct _range to set valid ranges for my output, when I init result I create mutiple struct of type range.</p> <p>Let say I have:</p> <p>Range1 -> 0.000 to 2.000 Ohm Range2 -> 2.01 to 20.00 Ohm Range3 -> 20.1 to 100.0 Ohm Range4 -> 101 to 200 Ohm</p> <p>When I init my result I create an array of 4 struct each containing this data </p> <pre><code>Range1 -&gt; struct _range { float from = 0.000 float to = 2.000 int decimal =3; int unit = Ohm; //Unit to format, Ohm in my example float div =1; }; Range2 -&gt; struct _range { float from = 2.01 float to = 20.00 int decimal =2; int unit = Ohm; //Unit to format, Ohm in my example float div =1; }; Range3 -&gt; struct _range { float from = 20.1 float to = 100.0 int decimal =1; int unit = Ohm; //Unit to format, Ohm in my example float div =1; }; Range4 -&gt; struct _range { float from = 101 float to = 200 int decimal =0; int unit = Ohm; //Unit to format, Ohm in my example float div =1; }; </code></pre> <p>Now Input to my function can be from 0 to 200, and I have to format text and round the float acording to ranges in the structures.</p> <p>I hope this explains it a bit more</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload