浮点格式的宽度选项是否在它是最后一个元素时排除小数点?

huangapple go评论68阅读模式
英文:

Does a floating point format's width option exclude the decimal point when it's the last element?

问题

我正在阅读关于printf的秘密以编写自己的printf的文档,并发现第二个"(%5.0f, e)",其中e = 2.718281828printf浮点格式及其输出令人困惑,为什么数字3和小数点前有四个空格?

我原以为输出应该是3.,数字3前有三个空格,因为小数点本身占据一个空格。

英文:

I'm reading this document about the secret of printf in order to write my own printf and I found the 2nd ("%5.0f.", e) with e = 2.718281828 printf floating point format and what it produces confusing, why is there four spaces before the 3 and the decimal point?

I thought the output would be 3. with three spaces before the number 3 as the decimal point takes one space itself.

答案1

得分: 1

在格式字符串中 "%5.0f." 有两部分:一个浮点数格式化说明符 %5.0f 和一个文字 .。这两部分没有任何连接。

第一部分打印出4个空格,然后是数字 2,而第二部分只是独立地打印出 .

英文:

In the format string "%5.0f." there are two parts: a floating point specifier %5.0f and a literal .. The two are not attached in any way.

The first part prints 4 spaces followed by the number 2, and the second part just prints . independently of the first part.

huangapple
  • 本文由 发表于 2023年5月29日 19:36:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定