Converting Engineering Units: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
m (Petty typo fix: s/your/you're/.)
 
(12 intermediate revisions by 3 users not shown)
Line 1: Line 1:
h1. '''Conversion of double values(0.0001)=>QString Units(100u) and Vice Versa.'''
[[Category:HowTo]]
This How-to explains how to convert double values to QString with units and vice Versa. If you're working in the technical domain surely there will be situation where you need to convert double values like 0.0001V into QString units like 100uV for display purpose in a {{DocLink|QLineEdit}} or {{DocLink|QLabel}}.


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 as a double values. But it will be easy if you have a function to do this conversions.


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.
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 convert into Qstring and add ''mV'' unit to it).


For scenario,
To make life easier, I created a code snippet with the functions <tt>convertToUnits</tt> and <tt>convertToValues</tt>. It can converts units from nano, micro, milli, deci, kilo and mega.


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).
<tt>convertToUnits</tt> takes double 0.1 as input and returns {{DocLink|QString}} 1000m.
<tt>convertToValues</tt> takes a QString 1000m as input and returns double 0.1.


To make life easier,i created a code snippet with functions convertToUnits &amp; convertToValues.It can converts units from nano, micro, milli, deci, kilo &amp; Mega.
<code>
QString MainWindow::convertToUnits(double l_nvalue) {
QString unit;
double value;


convertToUnits takes double 0.1 as input and returns Qstring 1000m.<br />convertToValues takes Qstring 1000m as input and returns double 0.1.
if(l_nvalue < 0) {
    value = l_nvalue * -1;
} else {
    value = l_nvalue;
}


Look at the image below for example,
if(value >= 1000000 && value < 1000000000) {
    value = value/1000000;
    unit = "M";
}
else if(value>=1000 && value<1000000){
    value = value/1000;
    unit = "K";
}
else if( value>=1 && value<1000) {
    value = value*1;
}
else if( (value*1000)>=1 && value<1000) {
    value = value*1000;
    unit = "m";
}
else if((value*1000000)>=1 && value<1000000){
    value = value*1000000;
    unit = QChar(0x00B5);
}
else if((value*1000000000)>=1 && value<1000000000){
    value = value*1000000000;
    unit = "n";
}


[[Image:https://dl.dropboxusercontent.com/u/12382973/testinQT.png|TestinQt]]
if(l_nvalue>0) {
    return (QString::number(value)+unit);
} else
if(l_nvalue<0) {
    return (QString::number(value*-1)+unit);
}


Functions code:
return QString::number(0);
}
</code>


<code>QString TestInQt::convertToUnits(double l_nvalue){<br /> QString unit;<br /> double value;
This is the function for converting the string representation of the value and unit to a double value:


if(l_nvalue&amp;lt;0){<br /> value=l_nvalue*–1;<br /> }else<br /> value=l_nvalue;
<code>
double convertToValues(const QString& input) {
    QRegExp r = QRegExp("^(.+)([nµumKMG])$");
    r.indexIn(input);


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 ( r.captureCount() == 2 ) {
<br /> QString unit,value;<br /> double inValue;<br /> bool ok=true;
<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];


inValue=value.toDouble(&amp;ok);
        QString strValue = r.cap(1);
        QString unit = r.cap(2);


if(unit[0]&amp;#39;n&amp;#39;)&amp;#123;
         bool ok = false;
            return(inValue/1000000000);
         double value = strValue.toDouble(&ok);
         &amp;#125;
         if ( !ok ) {
         else if((unit[0]QChar(0x00B5))||(unit[0]&amp;#39;u&amp;#39;))&amp;#123;
             return input.toDouble();
            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;
             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.


Hope it helped you.
        if ( unit == "n" ) {
            return (value*1e-9);
        } else
        if ( unit == "u" || unit == "µ" ) {
            return (value*1e-6);
        } else
        if ( unit == "m" ) {
            return (value*1e-3);
        } else
        if ( unit == "K" ) {
            return (value*1e3);
        } else
        if ( unit == "M" ) {
            return (value*1e6);
        } else
        if ( unit == "G" ) {
            return (value*1e9);
        }
 
        return value;
    }
 
    return 0;
}
</code>
 
As you can see here we use a regular expression for splitting the value and the unit. The value is then multiplied by the corresponding amount according to the unit prefix. Here we only cover a limited range of prefixes. For a full list see the [https://en.wikipedia.org/wiki/Unit_prefix Wikipedia entry]. In this context the difference between binary and metric prefixes might also be of interest.

Latest revision as of 14:15, 29 February 2016

This How-to explains how to convert double values to QString with units and vice Versa. If you're working in the technical domain surely there will be situation where you need to convert double values like 0.0001V into QString units like 100uV for display purpose in a QLineEdit or QLabel.

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

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 convert into Qstring and add mV unit to it).

To make life easier, I created a code snippet with the functions convertToUnits and convertToValues. It can converts units from nano, micro, milli, deci, kilo and mega.

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

QString MainWindow::convertToUnits(double l_nvalue) {
 QString unit;
 double value;

 if(l_nvalue < 0) {
     value = l_nvalue * -1;
 } else {
     value = l_nvalue;
 }

 if(value >= 1000000 && value < 1000000000) {
     value = value/1000000;
     unit = "M";
 }
 else if(value>=1000 && value<1000000){
     value = value/1000;
     unit = "K";
 }
 else if( value>=1 && value<1000) {
     value = value*1;
 }
 else if( (value*1000)>=1 && value<1000) {
     value = value*1000;
     unit = "m";
 }
 else if((value*1000000)>=1 && value<1000000){
     value = value*1000000;
     unit = QChar(0x00B5);
 }
 else if((value*1000000000)>=1 && value<1000000000){
     value = value*1000000000;
     unit = "n";
 }

 if(l_nvalue>0) {
     return (QString::number(value)+unit);
 } else
 if(l_nvalue<0) {
     return (QString::number(value*-1)+unit);
 }

 return QString::number(0);
}

This is the function for converting the string representation of the value and unit to a double value:

double convertToValues(const QString& input) {
    QRegExp r = QRegExp("^(.+)([nµumKMG])$");
    r.indexIn(input);

    if ( r.captureCount() == 2 ) {

        QString strValue = r.cap(1);
        QString unit = r.cap(2);

        bool ok = false;
        double value = strValue.toDouble(&ok);
        if ( !ok ) {
            return input.toDouble();
        }

        if ( unit == "n" ) {
            return (value*1e-9);
        } else
        if ( unit == "u" || unit == "µ" ) {
            return (value*1e-6);
        } else
        if ( unit == "m" ) {
            return (value*1e-3);
        } else
        if ( unit == "K" ) {
            return (value*1e3);
        } else
        if ( unit == "M" ) {
            return (value*1e6);
        } else
        if ( unit == "G" ) {
            return (value*1e9);
        }

        return value;
    }

    return 0;
}

As you can see here we use a regular expression for splitting the value and the unit. The value is then multiplied by the corresponding amount according to the unit prefix. Here we only cover a limited range of prefixes. For a full list see the Wikipedia entry. In this context the difference between binary and metric prefixes might also be of interest.