[c++] 指定精度格式化

1.整数指定格式

格式“%03d” ,如何把其中的03能够指定0-9之间的任何一个数呢?

方法1:

把“%03d”逐个字符进行展开,%03d --%%  0  %d  d

然后整体当作格式使用,

举例:

    int aa = 2;
	
	int nDotNum = 3;

	CString ss1;
	CString ss2;

	//ss2.format(L"%03d", aa);

	//%03d --%% 0 %d d

	ss1.Format(L"%%0%dd", nDotNum);

	ss2.Format(ss1, aa);

方法2:用"*"代替整数,整体一句话就解决,

“ %03d --> %*d

举例:

    int aa = 2;
	
	int nDotNum = 3;

	CString ss1;
	CString ss2;

    //%03d --%*d
	ss2.Format(L"%0*d", nDotNum, aa);

2.float

%.3f   --%% . %d f

%.3f   --%.* f

举例:

    float bb = 1.123456;
    int nDotNum = 3;

	CString ss1;
	CString ss2;

    //%.3f   --%% . %d f
	ss1.Format(L"%%.%df", nDotNum);

	ss2.Format(ss1, bb);

	//%.3f   --%.* f
	ss2.Format(L"%.*f", nDotNum, bb);

3.double

%.3lf   --%% . %d lf

%.3lf   --%.* lf

举例:

    double cc = 2.987654;

    int nDotNum = 3;

	CString ss1;
	CString ss2;

    //%.3lf   --%% . %d lf
	ss1.Format(L"%%.%dlf", nDotNum);

	ss2.Format(ss1, cc);

	//%.3lf   --%.* lf
	ss2.Format(L"%.*lf", nDotNum, cc);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值