【问题标题】:How to print a double in C++如何在 C++ 中打印双精度
【发布时间】:2018-02-19 03:09:49
【问题描述】:

我正在做一个 C++ 练习。它需要我打印一个双,但我已经尝试了几次以下代码,它没有工作。如何在以下代码中将 GPA 打印为双精度?

#include <iostream>
#include <string>
using namespace std;
class gradeRecord{
private:
    string studentID;
    int units,gradepts;

public:    
    gradeRecord(string stuID, int unts, int gpts){
        studentID = stuID;
        units = unts;
        gradepts = gpts;
    }

    double gpa(){
        int gpa;
        gpa = double(gradepts)/units;
        return gpa;
    }

    void updateGradeInfo(int unts,int gpts){
        units = unts;
        gradepts = gpts;
    }

    void writeGradeInfo(){
        cout << "Student:" << studentID << "\t" 
            << "Unit:" << units << "\t" 
            << "GradePts:" << gradepts << "\t"
            << "GPA:" << gpa();
    }

};

int main(){
    gradeRecord studObj("783-29-4716", 100, 345);
    studObj.writeGradeInfo();
    return 0;
}

结果是 "学生:783-92-4716 学分:100 GradePts:345 GPA:3"

但我期望的是 "学生:783-92-4716 学分:100 GradePts:345 GPA:3.45"

我怎样才能在 GPA 中得到一个整数,而不是一个整数?

【问题讨论】:

  • 您将gpa 定义为int,将其定义为double
  • 考虑的替代方案 - 使用整数数学(因为它很容易): int gpa = (100 *gradepts) / units;然后 std::cout
  • @DOUGLASO.MOEN:如果(gpa % 100) 只是一个数字,结果就不会那么好了。
  • @DifengChen 你可以查看下面的解决方案,看看它是否能解决你的问题。
  • @BenVoigt - 故意的......只是为了说明修复(和修复(和修复))的容易程度。 std::cout

标签: c++ printing int double


【解决方案1】:

我怎样才能在 GPA 中得到一个整数,而不是一个整数?

当你使用

    int gpa;
    gpa = double(gradepts)/units;

您正在截断double

如果要保留至少两位小数,可以使用:

double gpa(){
    int gpa = 100*gradepts/units;
    return gpa/100.0;
}

【讨论】:

    【解决方案2】:

    您可以通过包含一个操纵器轻松完成。这个操纵器在标题&lt;iomanip&gt; 中声明。并直接在std::cout 上设置精度并使用std::fixed 格式说明符。

    #include <iomanip>      // std::setprecision
    
      double gpa(){
      int gpa = 100*gradepts/units;
      std::cout << std::setprecision(3) << gpa/100.0 << '\n'; // you can set your precission to a value you plan to use
      std::cout << std::fixed;
        return gpa/100.0;
    }
    

    这应该使您更正的工作成为:

    #include <iostream>
    #include <iomanip>      // std::setprecision
    
    
    using namespace std;
    class gradeRecord{
    private:
        string studentID;
        int units,gradepts;
    
    public:    
        gradeRecord(string stuID, int unts, int gpts){
            studentID = stuID;
            units = unts;
            gradepts = gpts;
        }
    
          double gpa(){
          int gpa = 100*gradepts/units;
          std::cout << std::setprecision(3) << gpa/100.0 << '\n'; // you can set your precission to a value you plan to use
          std::cout << std::fixed;
            return gpa/100.0;
        }
    
        void updateGradeInfo(int unts,int gpts){
            units = unts;
            gradepts = gpts;
        }
    
        void writeGradeInfo(){
            cout << "Student:" << studentID << "\t" 
                << "Unit:" << units << "\t" 
                << "GradePts:" << gradepts << "\t"
                << "GPA:" << gpa();
        }
    
    };
    
    int main(){
        gradeRecord studObj("783-29-4716", 100, 345);
        studObj.writeGradeInfo();
        return 0;
    }
    

    我希望这能解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 2016-04-30
      • 1970-01-01
      相关资源
      最近更新 更多