[Sharing]Converting Units=>Values & Values=>Units



  • Hi,
    Sometimes we need to convert the units like milli, micro, nano, kilo etc to values for actual calulation.And again sometimes we need to convert the actual values to milli, micro, nano for display purpose.This code will be useful for converting both to and fro.its simple code but like to share with you all.

    For example if you give values like 0.001 it will convert into 1m and you can add V,A,Ohms etc with it to give 1mV.
    For example if you give units like 1mV it will convert it into 0.001 values.

    @QString TestInQt::convertToUnits(double value){
    QString unit;

    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";
    }
    return (QString::number(value)+unit);
    

    }
    double TestInQt::convertToValues(QString input){

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

    }@



  • small changes in the converToUnits function code for negative value validation.
    @QString TestInQt::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);
    if(l_nvalue<0)
        return (QString::number(value*-1)+unit);
    

    }@



  • The code snippet seems useful. Why don't you create an article about it at the wiki?

    We already have a lot of other code snippets at the wiki :)



  • yea ok.but how to do that... or link to write in wiki?



  • [quote author="dreamerindia" date="1376374464"]yea ok.but how to do that... or link to write in wiki?[/quote]

    Just go to the "wiki":http://qt-project.org/wiki and click Create new page (it is situated at the wiki menu on the right side of the screen)



  • yea i got it thanks:-)





  • Hi leon.anavi,
    how to change the name of article

    http://qt-project.org/wiki/dreamerindia

    to

    http://qt-project.org/wiki/Converting_Units=>Values_Values=>Units



  • [quote author="dreamerindia" date="1376384265"]Hi leon.anavi,
    how to change the name of article

    http://qt-project.org/wiki/dreamerindia

    to

    http://qt-project.org/wiki/Converting_Units=>Values_Values=>Units[/quote]

    I have create a "new wiki article":http://qt-project.org/wiki/Converting-Units and the old just redirects to it.



  • Thank you very much


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.