l********y 发帖数: 1327 | 1 输入一个long double,格式化后输出。例如: 234567614.98 -> 234,567,
614.98
-给出test case
方法是,
Char a[200];
Sprintf(a,”%Lf”,d);
但是得到的c string数组后面总有0000的结尾,比如输入2345.98得到2345.980000,有
什么办法可以把这些后缀的0去掉呢? | l********y 发帖数: 1327 | 2 主要是不清楚sprintf总是会得到后缀是0000的c string吗?如果每次都是0000结尾那
倒反而好了,如果不是那就不明白如何判断了 | h**6 发帖数: 4160 | | l********y 发帖数: 1327 | 4 用了sprintf(a,".2Lf",d),结果出来的c string数组是1234.98D\01这样的,不知道为
什么已经限制精度为2了,还是出来几个字符非要补齐6位? | s*****e 发帖数: 16824 | 5 试一下限制位数总长,比如6.2Lf.
【在 l********y 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 用了sprintf(a,".2Lf",d),结果出来的c string数组是1234.98D\01这样的,不知道为 : 什么已经限制精度为2了,还是出来几个字符非要补齐6位?
| l********y 发帖数: 1327 | 6 好像不行,而且如何事先知道long double转化为c string以后的长度? | p******z 发帖数: 38 | 7 有方法限制吗?
如果没有的话,先转成字符串再操作?对字符串的操作方法很多的 | d*********g 发帖数: 59 | 8 如果转换成字符串,那转换成的字符串里岂不是不知道有多少个0000 | t****t 发帖数: 6806 | 9 you are totally missing the point -- the question asked you to add "," as
the thousands' grouping character. printf() family is not guaranteed to do
that (i.e. implementation-defined), and if it does, it also depends on
locale and some special flag ("%'f" for SUSv2, for example). so you are not
supposed to use printf() family (i.e. whoever asked you the question do not
expect you to do that). you are supposed to do it manually.
as for the trailing zero, you need to give more information -- probably
depends on whether the input is send in via a string (e.g. from cin) or via
a long double variable. if it is sent in via string, simply don't involve a
long double, process the string and add "," wherever it should be. if it is
sent in via long double, you'll need to know the output precision (given or
you may assume a default value), otherwise long double could be quite long
that you don't know whether a trailing number is really there or just error.
【在 l********y 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 输入一个long double,格式化后输出。例如: 234567614.98 -> 234,567, : 614.98 : -给出test case : 方法是, : Char a[200]; : Sprintf(a,”%Lf”,d); : 但是得到的c string数组后面总有0000的结尾,比如输入2345.98得到2345.980000,有 : 什么办法可以把这些后缀的0去掉呢?
|
|