Converting Engineering Units: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Convert ExpressionEngine section headers)
(Style, Wording)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
[[Category:HowTo]]
== Introduction ==
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 {{DocLink|QLineEdit}} or {{DocLink|QLabel}}.


= '''Conversion of double values(0.0001)=>QString Units(100u) and Vice Versa.''' =
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 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.
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>


convertToUnits takes double 0.1 as input and returns Qstring 1000m.
QString TestInQt::convertToUnits(double l_nvalue) {
convertToValues takes Qstring 1000m as input and returns double 0.1.
 
Look at the image below for example,
 
[[Image:https://dl.dropboxusercontent.com/u/12382973/testinQT.png|TestinQt]]
 
Functions code:
 
<code>QString TestInQt::convertToUnits(double l_nvalue){
  QString unit;
  QString unit;
  double value;
  double value;


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


if(value>=1000000&amp;amp;&amp;value<1000000000){
if(value>=1000000 && value<1000000000){
  value=value/1000000; unit="M";
  value=value/1000000; unit="M";
  }
  }
  else if(value>=1000&amp;amp;&amp;value<1000000){
  else if(value>=1000 && value<1000000){
  value=value/1000; unit="K";
  value=value/1000; unit="K";
  }
  }
  else if((value>=1&amp;amp;&amp;value<1000))
  else if((value>=1 && value<1000))
  value=value*1;
  value=value*1;
  else if((value*1000)>=1&amp;amp;&amp;value<1000){
  else if((value*1000)>=1 && value<1000){
  value=value*1000; unit="m";
  value=value*1000; unit="m";
  }
  }
  else if((value*1000000)>=1&amp;amp;&amp;value<1000000){
  else if((value*1000000)>=1 && value<1000000){
  value=value*1000000; unit=QChar(0x00B5);
  value=value*1000000; unit=QChar(0x00B5);
  }
  }
  else if((value*1000000000)>=1&amp;amp;&amp;value<1000000000){
  else if((value*1000000000)>=1 && value<1000000000){
  value=value*1000000000; unit="n";
  value=value*1000000000; unit="n";
  }
  }
Line 52: Line 45:
  return (QString::number(value*–1)''unit);
  return (QString::number(value*–1)''unit);
}
}
double TestInQt::convertToValues(QString input){
double TestInQt::convertToValues(QString input){
  QString unit,value;
  QString unit,value;
  double inValue;
  double inValue;
Line 61: Line 54:


  for(int i=0;i<=input.count();i){
  for(int i=0;i<=input.count();i){
  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))){
  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];
  unit[j]=input[i];
  j;
  j;
  }
  }
  }
  }
  for(int k=0;k<(input.count()-unit.count());k''+)
  for(int k=0;k<(input.count()-unit.count());k++)
  value[k]=input[k];
  value[k]=input[k];


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


if(unit[0]&amp;#39;n&amp;#39;){
if(unit[0]&amp;#39;n&amp;#39;){
Line 91: Line 84:
}
}
</code>
</code>
I also attached the [https://dl.dropboxusercontent.com/u/12382973/testInQt.tar.gz Source Code] project file.
Hope it helped you.

Revision as of 13:24, 10 March 2015

Introduction

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

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]&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'){
 return(inValue/1000);
 }
 else if(unit[0]&amp;#39;K&amp;#39;){
            return(inValue*1000);
        }
        else if(unit[0]'M'){
 return(inValue*1000000);
 }
 else{
 return(inValue*1);
 }
}