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]
  
  
  
  
  
相关文章
相关标签/搜索