Converting Engineering Units: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Improved String to Double Function)
Line 56: Line 56:
}
}


double TestInQt::convertToValues(QString input){
double convertToValues(const QString& input) {
QString unit,value;
    QRegExp r = QRegExp("^(.+)([nµumKMG])$");
double inValue;
    r.indexIn(input);
bool ok=true;


int j=0;
    if ( r.captureCount() == 2 ) {


for(int i=0;i<=input.count();i){
        QString strValue = r.cap(1);
if((input[i]>='A' && input[i]<='Z')||(input[i]>='a' && input[i]<='z')||(input[i]QChar(0x2126))||(input[i]QChar(0x00B5))){
        QString unit = r.cap(2);
unit[j]=input[i];
j;
}
}
for(int k=0;k<(input.count()-unit.count());k++)
value[k]=input[k];


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


if(unit[0]&amp;#39;n&amp;#39;){
        if ( unit == "n" ) {
             return(inValue/1000000000);
            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);
         }
         }
        else if((unit[0]QChar(0x00B5))||(unit[0]&amp;#39;u&amp;#39;)){
 
            return(inValue/1000000);
         return value;
         }
    }
        else if(unit[0]'m'){
 
return(inValue/1000);
    return 0;
}
else if(unit[0]&amp;#39;K&amp;#39;){
            return(inValue*1000);
        }
        else if(unit[0]'M'){
return(inValue*1000000);
}
else{
return(inValue*1);
}
}
}
</code>
</code>

Revision as of 14:58, 12 March 2015

This How-to explains how to convert double values to QString with units 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 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);
}

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;
}