Converting Engineering Units: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
h1. '''Conversion of double values(0.0001)=>QString Units(100u) and Vice Versa.'''
h1. '''Conversion of double values(0.0001)=>QString Units(100u) and Vice Versa.'''


If your working in the technical domain surely there will be situation where you need to convert double values like 0.0001V into QString units data like(100uV) for display purpose.For example to set in QLineEdit,QLabel etc..
If your working in the technical domain surely there will be situation where you need to convert double values like 0.0001V into QString units data like(100uV) for display purpose.For example to set in QLineEdit,QLabel etc..
Line 21: Line 21:
<code>QString TestInQt::convertToUnits(double l_nvalue){<br /> QString unit;<br /> double value;
<code>QString TestInQt::convertToUnits(double l_nvalue){<br /> QString unit;<br /> double value;


if(l_nvalue&amp;lt;0){<br /> value=l_nvalue*–1;<br /> }else<br /> value=l_nvalue;
if(l_nvalue<0){<br /> value=l_nvalue*–1;<br /> }else<br /> value=l_nvalue;


if(value&amp;gt;=1000000&amp;amp;&amp;value&amp;lt;1000000000){<br /> value=value/1000000; unit=&quot;M&amp;quot;;<br /> }<br /> else if(value&amp;gt;=1000&amp;amp;&amp;value&amp;lt;1000000){<br /> value=value/1000; unit=&quot;K&amp;quot;;<br /> }<br /> else if((value&amp;gt;=1&amp;amp;&amp;value&amp;lt;1000))<br /> value=value*1;<br /> else if((value*1000)&gt;=1&amp;amp;&amp;value&amp;lt;1000){<br /> value=value*1000; unit=&quot;m&amp;quot;;<br /> }<br /> else if((value*1000000)&gt;=1&amp;amp;&amp;value&amp;lt;1000000){<br /> value=value*1000000; unit=QChar(0x00B5);<br /> }<br /> else if((value*1000000000)&gt;=1&amp;amp;&amp;value&amp;lt;1000000000){<br /> value=value*1000000000; unit=&quot;n&amp;quot;;<br /> }<br /> if(l_nvalue&amp;gt;0)<br /> return (QString::number(value)+unit);<br /> if(l_nvalue&amp;lt;0)<br /> return (QString::number(value*–1)''unit);<br />}<br />double TestInQt::convertToValues(QString input){
if(value>=1000000&amp;amp;&amp;value<1000000000){<br /> value=value/1000000; unit="M";<br /> }<br /> else if(value>=1000&amp;amp;&amp;value<1000000){<br /> value=value/1000; unit="K";<br /> }<br /> else if((value>=1&amp;amp;&amp;value<1000))<br /> value=value*1;<br /> else if((value*1000)>=1&amp;amp;&amp;value<1000){<br /> value=value*1000; unit="m";<br /> }<br /> else if((value*1000000)>=1&amp;amp;&amp;value<1000000){<br /> value=value*1000000; unit=QChar(0x00B5);<br /> }<br /> else if((value*1000000000)>=1&amp;amp;&amp;value<1000000000){<br /> value=value*1000000000; unit="n";<br /> }<br /> if(l_nvalue>0)<br /> return (QString::number(value)+unit);<br /> if(l_nvalue<0)<br /> return (QString::number(value*–1)''unit);<br />}<br />double TestInQt::convertToValues(QString input){
<br /> QString unit,value;<br /> double inValue;<br /> bool ok=true;
<br /> QString unit,value;<br /> double inValue;<br /> bool ok=true;
<br /> int j=0;
<br /> int j=0;
<br /> for(int i=0;i&amp;lt;=input.count();i){<br /> if((input[i]&gt;='A'&amp;&amp;input[i]&lt;='Z')||(input[i]&gt;='a'&amp;&amp;input[i]&lt;='z')||(input[i]QChar(0x2126))||(input[i]QChar(0x00B5))){<br /> unit[j]=input[i];<br /> j;<br /> }<br /> }<br /> for(int k=0;k&amp;lt;(input.count()-unit.count());k''+)<br /> value[k]=input[k];
<br /> for(int i=0;i<=input.count();i){<br /> if((input[i]>='A'&amp;&amp;input[i]<='Z')||(input[i]>='a'&amp;&amp;input[i]<='z')||(input[i]QChar(0x2126))||(input[i]QChar(0x00B5))){<br /> unit[j]=input[i];<br /> j;<br /> }<br /> }<br /> for(int k=0;k<(input.count()-unit.count());k''+)<br /> value[k]=input[k];


inValue=value.toDouble(&amp;ok);
inValue=value.toDouble(&amp;ok);


if(unit[0]&amp;#39;n&amp;#39;)&amp;#123;
if(unit[0]&amp;#39;n&amp;#39;){
             return(inValue/1000000000);
             return(inValue/1000000000);
         &amp;#125;
         }
         else if((unit[0]QChar(0x00B5))||(unit[0]&amp;#39;u&amp;#39;))&amp;#123;
         else if((unit[0]QChar(0x00B5))||(unit[0]&amp;#39;u&amp;#39;)){
             return(inValue/1000000);
             return(inValue/1000000);
         &amp;#125;
         }
         else if(unit[0]'m'){<br /> return(inValue/1000);<br /> }<br /> else if(unit[0]&amp;#39;K&amp;#39;)&amp;#123;
         else if(unit[0]'m'){<br /> return(inValue/1000);<br /> }<br /> else if(unit[0]&amp;#39;K&amp;#39;){
             return(inValue*1000);
             return(inValue*1000);
         &amp;#125;
         }
         else if(unit[0]'M'){<br /> return(inValue*1000000);<br /> }<br /> else{<br /> return(inValue*1);<br /> }<br />}<br /></code><br />I also attached the &quot;Source Code&amp;quot;:https://dl.dropboxusercontent.com/u/12382973/testInQt.tar.gz project file.
         else if(unit[0]'M'){<br /> return(inValue*1000000);<br /> }<br /> else{<br /> return(inValue*1);<br /> }<br />}<br /></code><br />I also attached the "Source Code":https://dl.dropboxusercontent.com/u/12382973/testInQt.tar.gz project file.


Hope it helped you.
Hope it helped you.

Revision as of 14:22, 24 February 2015

h1. Conversion of double values(0.0001)=>QString Units(100u) and Vice Versa.

If your working in the technical domain surely there will be situation where you need to convert double values like 0.0001V into QString units data like(100uV) for display purpose.For example to set in QLineEdit,QLabel etc..

And again for actual calculation you need it in double values.But it will be easy if you have a function to do this conversions.

For scenario,

If you need to increment/decrement the 100mV, first you need to convert Qstring 100mV into double 0.1(crop the 100 from 100mV,divide by 1000 to get 0.1) and then increment/decrement and again we need to convert into incremented 101mV(multiply 0.1 into 1000 and add convert into Qstring and add “mV” unit to it).

To make life easier,i created a code snippet with functions convertToUnits & convertToValues.It can converts units from nano, micro, milli, deci, kilo & Mega.

convertToUnits takes double 0.1 as input and returns Qstring 1000m.
convertToValues takes Qstring 1000m as input and returns double 0.1.

Look at the image below for example,

TestinQt

Functions code:

QString TestInQt::convertToUnits(double l_nvalue){<br /> QString unit;<br /> double value;

if(l_nvalue<0){<br /> value=l_nvalue*1;<br /> }else<br /> value=l_nvalue;

if(value>=1000000&amp;amp;&amp;value<1000000000){<br /> value=value/1000000; unit="M";<br /> }<br /> else if(value>=1000&amp;amp;&amp;value<1000000){<br /> value=value/1000; unit="K";<br /> }<br /> else if((value>=1&amp;amp;&amp;value<1000))<br /> value=value*1;<br /> else if((value*1000)>=1&amp;amp;&amp;value<1000){<br /> value=value*1000; unit="m";<br /> }<br /> else if((value*1000000)>=1&amp;amp;&amp;value<1000000){<br /> value=value*1000000; unit=QChar(0x00B5);<br /> }<br /> else if((value*1000000000)>=1&amp;amp;&amp;value<1000000000){<br /> value=value*1000000000; unit="n";<br /> }<br /> if(l_nvalue>0)<br /> return (QString::number(value)+unit);<br /> if(l_nvalue<0)<br /> return (QString::number(value*1)''unit);<br />}<br />double TestInQt::convertToValues(QString input){
<br /> QString unit,value;<br /> double inValue;<br /> bool ok=true;
<br /> int j=0;
<br /> for(int i=0;i<=input.count();i){<br /> if((input[i]>='A'&amp;&amp;input[i]<='Z')||(input[i]>='a'&amp;&amp;input[i]<='z')||(input[i]QChar(0x2126))||(input[i]QChar(0x00B5))){<br /> unit[j]=input[i];<br /> j;<br /> }<br /> }<br /> for(int k=0;k<(input.count()-unit.count());k''+)<br /> value[k]=input[k];

inValue=value.toDouble(&amp;ok);

if(unit[0]&amp;#39;n&amp;#39;){
            return(inValue/1000000000);
        }
        else if((unit[0]QChar(0x00B5))||(unit[0]&amp;#39;u&amp;#39;)){
            return(inValue/1000000);
        }
        else if(unit[0]'m'){<br /> return(inValue/1000);<br /> }<br /> else if(unit[0]&amp;#39;K&amp;#39;){
            return(inValue*1000);
        }
        else if(unit[0]'M'){<br /> return(inValue*1000000);<br /> }<br /> else{<br /> return(inValue*1);<br /> }<br />}<br />


I also attached the "Source Code":https://dl.dropboxusercontent.com/u/12382973/testInQt.tar.gz project file.

Hope it helped you.