码迷,mamicode.com
首页 > 编程语言 > 详细

c/c++ 浮点型处理

时间:2016-08-18 09:55:12      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:

#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <sstream>
#include <iomanip>
using namespace std;

double round_price(double price, int dotnum)
{
    double out_price = 0.0;
    char buf[10];
    memset(buf, 0, 0);
    std::string str("%.");
    sprintf(buf, "%d", dotnum);
    str += buf;
    str += "lf";
    memset(buf, 0, 0);
    double cal_price = price + 0.0000001;

    sprintf(buf, str.c_str(), cal_price);
    cout << buf << endl;

    sscanf(buf, "%lf", &out_price);
    return out_price;
}

int main()
{
    double dd = 5.6666666666;
    double two = round_price(dd, 2);

    cout << two << endl;
    stringstream stream;
    stream <<setiosflags(ios::fixed);
    stream.precision(2);

    stream << 5.665111;

    double one;
    stream >> one;

    cout << one << endl;

    return 0;
}

 

c/c++ 浮点型处理

标签:

原文地址:http://www.cnblogs.com/kaishan1990/p/5782726.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!