主要内容

介绍C++中如何格式化输出浮点数。

控制浮点数输出格式需要包含iomanip头文件。

使用fixed来控制输出的浮点数的小数位是固定的。可参考http://en.cppreference.com/w/cpp/io/manip/fixed

使用right和left来控制输出的对其位置。

使用setw来控制后面一个数据占用多少个单位宽度。

代码

#include <iostream>
#include <iomanip>
using namespace std;

int main(int argc, char** argv){
    double salary = 0.;
    double tax_percent = 0.01;
    cout << "input Milk's salary please : ";
    cin >> salary;      

    cout << std::fixed;
    cout << std::setprecision(2);

    cout << std::left  << setw(15) << "Name";
    cout << std::right << setw(15) << "Salary";
    cout << std::right << setw(15) << "tax";
    cout << endl;

    cout << std::left  << setw(15) << "Milk";
    cout << std::right << setw(15) << salary;
    cout << std::right << setw(15) << salary*tax_percent;
    cout << endl;
    return EXIT_SUCCESS;
}

结果

C++格式化输出浮点数的更多相关文章

  1. [转载] c++ cout 格式化输出浮点数、整数及格方法

    C语言里可以用printf(),%f来实现浮点数的格式化输出,用cout呢...? 下面的方法是在网上找到的,如果各位有别的办法谢谢留下... iomanip.h是I/O流控制头文件,就像C里面的格式 ...

  2. [ZZ]c++ cout 格式化输出浮点数、整数及格式化方法

    C语言里可以用printf(),%f来实现浮点数的格式化输出,用cout呢...?下面的方法是在网上找到的,如果各位有别的办法谢谢留下... iomanip.h是I/O流控制头文件,就像C里面的格式化 ...

  3. //%f表示以十进制格式化输出浮点数 %.2f

    //%f表示以十进制格式化输出浮点数 String s1 ="评分: %.1f"; String s2 = String.format(s1, 8.0); System.out.p ...

  4. [No000063]Python格式化输出

    python print格式化输出. 1. 打印字符串 print ("His name is %s"%("Aviad")) 效果: 2.打印整数 print ...

  5. Python学习教程(learning Python)--1.2.2 Python格式化输出基础

    本节讨论为何要格式化输出数据? 先看一段代码吧,本程序的功能是计算月支付金额. amount_due = 5000.0 #年支付金额 monthly_payment = amount_due / 12 ...

  6. Python之格式化输出讲解

    1.格式化输出整数python print也支持参数格式化,与C言的printf似, strHello = "the length of (%s) is %d" %(Hello W ...

  7. 7.20.01 java格式化输出 printf 例子

    java格式化输出 printf 例子 importjava.util.Date; publicclassPrintf { publicstaticvoidmain(String[] args) { ...

  8. Python print函数用法,print 格式化输出

    原文地址:http://blog.csdn.net/zanfeng/article/details/52164124 使用print输出各型的 字符串 整数 浮点数 出度及精度控制 strHello ...

  9. Java面向对象 第2节 Scanner 类和格式化输出printf

    §Scanner 类 java.util.Scanner 是 Java5 的新特征,我们可以通过 Scanner 类来获取用户的输入. 1.创建 Scanner 对象的基本语法:Scanner s = ...

随机推荐

  1. round()函数 浮点数的四舍五入

    浮点数的四舍五入 print round(1.7333) 2.0

  2. 初探Javascript之Canvas

    什么是Canvas <canvas>是 HTML5 新增的元素,可使用JavaScript脚本来绘制图形. canvas是一个矩形区域,您可以控制其每一像素. 引入Canvas ```ht ...

  3. [LeetCode] Second Minimum Node In a Binary Tree 二叉树中第二小的结点

    Given a non-empty special binary tree consisting of nodes with the non-negative value, where each no ...

  4. [LeetCode] Output Contest Matches 输出比赛匹配对

    During the NBA playoffs, we always arrange the rather strong team to play with the rather weak team, ...

  5. vector数组中STL习惯性用法

    参考:https://blog.csdn.net/lcamisak/article/details/79358060

  6. 实验吧_who are you?(盲注)

    who are you? 翻翻源码,抓抓包,乱试一通都没有什么结果 题目中提示有ip,立马应该联想到X-Forwarded-For 虽然知道是这个方面的题,但完全不知道从何入手,悄咪咪去翻一下wp 才 ...

  7. Docker入门之--基础知识

    1.先是在Mac上安装. 按照这两个就可以很简单的完成 https://docs.docker.com/docker-for-mac/ https://docs.docker.com/docker-f ...

  8. [JSOI 2008]最大数

    Description 题库链接 给你一个序列,初始为空.资瓷下列操作: 在序列末尾加上一个数: 查询后 \(L\) 个数中的最大值. 操作总数为 \(m\) , \(1\leq m\leq 2000 ...

  9. codefroces 612E Square Root of Permutation

    A permutation of length n is an array containing each integer from 1 to n exactly once. For example, ...

  10. 洛谷mNOIP模拟赛Day1-分组

    传送门 首先是贪心的思路 从后向前选,能多选就多选, 理由:数字越少肯定越优,同时间隔尽量向前推,字典序尽量小 对于K==1,枚举1~512直接判断 对于K==2,需要用镜像并查集,来刻画" ...