c++中的一些計算的問題

要實現小數的四捨五入, php

float a = 3.456; //保留到小數點後兩位
float b =(int)((a * 100) + 0.5) / 100.0;

可是這樣對負數很差使, 對負數的話, 這個帖子裏還有辦法: [C] C語言如何實現浮點數的四捨五入?html

 

int, float, double 與QString的轉化: Qt中 int ,float ,double轉換爲QStringspa

 

QString與string的相互轉換:.net

一、QString與int相互轉換unix

QString qstr = QString::number(123);htm

int i = atoi(qstr.toStdString().c_str());blog

也能夠這樣:int i = atoi(qstr.ascii());ci

二、QString與string,即std::string字符串

string s = qstr.toStdString();get

QString qstr2 = QString::fromStdString(s);

 

 

 

關於保留幾位小數並轉化成字符串的幾種辦法:

1) 要包含 <stdlib.h>

//    Trans = (int(Trans*10+0.5))/10.0;  //四捨五入
//    char buffer[20];
//    gcvt(Trans, 15, buffer);      //把浮點數轉換成字符串

//    timeOutput->setText(buffer);        //最大燃燒時間:[s]

  
  
  
  
  //    double Ll = int(Rcpp::as<double>(m_R["Ll"])*100+0.5)/100.0;//(int(Rcpp::as<double>(m_R["Ll"]))*100)/100.0;      //四捨五入
//    double Xit1 = int(Rcpp::as<double>(m_R["Xit1"])*100+0.5)/100.0; //(int(Rcpp::as<double>(m_R["Xit1"]))*100)/100.00;
//    double De = int(Rcpp::as<double>(m_R["De"])*10+0.5)/10.0;//int(Rcpp::as<double>(m_R["De"]))*10/10.0;

  
  
  
  
  //gcvt(Xit1,15,buffer);
  
  
  
  
  

  2) 

QString PoolFire::Round(double data, int prec){
    std::stringstream ss;
    ss<<std::fixed<<std::setprecision(prec)<<data;
    std::string s = ss.str();
    return QString::fromStdString(s);
}

//使用
  
  
  
  
      LOutput->setText(Round(Ll,2));         //火焰長度L:[m]
    D2Output->setText(Round(Xit1,2));          //火焰傾角r:[°]
    rOutput->setText(Round(De,1));           //火焰後拖量D':[m]
  
  
  
  
  
相關文章
相關標籤/搜索