Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [Sharing]Converting Units=>Values & Values=>Units

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

Scheduled Pinned Locked Moved Mobile and Embedded
10 Posts 2 Posters 3.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dreamerindia
    wrote on last edited by
    #1

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

    }@

    ~Ravivarman~

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dreamerindia
      wrote on last edited by
      #2

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

      }@

      ~Ravivarman~

      1 Reply Last reply
      0
      • L Offline
        L Offline
        leon.anavi
        wrote on last edited by
        #3

        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 :)

        http://anavi.org/

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dreamerindia
          wrote on last edited by
          #4

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

          ~Ravivarman~

          1 Reply Last reply
          0
          • L Offline
            L Offline
            leon.anavi
            wrote on last edited by
            #5

            [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)

            http://anavi.org/

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dreamerindia
              wrote on last edited by
              #6

              yea i got it thanks:-)

              ~Ravivarman~

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dreamerindia
                wrote on last edited by
                #7

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

                ~Ravivarman~

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dreamerindia
                  wrote on last edited by
                  #8

                  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

                  ~Ravivarman~

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    leon.anavi
                    wrote on last edited by
                    #9

                    [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.

                    http://anavi.org/

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      dreamerindia
                      wrote on last edited by
                      #10

                      Thank you very much

                      ~Ravivarman~

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved