Skip to content

Commit f02fc53

Browse files
authored
Merge pull request #655 from devlights:add-formatting-numbers-example
Add examples/basic/formatting/numbers.go
2 parents 319d697 + 8aeb156 commit f02fc53

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

examples/basic/formatting/examples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ func (r *register) Regist(m mapping.ExampleMapping) {
2020
m["formatting_appendf"] = AppendF
2121
m["formatting_appendln"] = AppendLn
2222
m["formatting_padding_arbitary_length"] = PaddingArbitaryLength
23+
m["formatting_numbers"] = Numbers
2324
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package formatting
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
// Numbers は、数値のフォーマットについてのサンプルです.
10+
//
11+
// # REFERENCES
12+
// - https://pkg.go.dev/fmt
13+
func Numbers() error {
14+
// '+' を付与すると常に符号が表示される
15+
{
16+
x := 123
17+
fmt.Printf("%+d\n", x)
18+
fmt.Printf("%+d\n", x*-1)
19+
}
20+
output.StdoutHr()
21+
22+
// 指定した値分だけ右側をスペースで埋める
23+
{
24+
x := 123
25+
fmt.Printf("'%-10d'\n", x)
26+
fmt.Printf("'%-10d'\n", x*-1)
27+
}
28+
output.StdoutHr()
29+
30+
// 別の進数で出力
31+
{
32+
x := 0xff
33+
fmt.Printf("%d\n", x)
34+
fmt.Printf("%#b\n", x)
35+
fmt.Printf("%#x\n", x)
36+
fmt.Printf("%#o\n", x)
37+
}
38+
output.StdoutHr()
39+
40+
// 要素毎にスペースを開けて出力
41+
{
42+
fmt.Printf("% x\n", [...]byte{253, 254, 255})
43+
fmt.Printf("% x\n", []byte{250, 251, 252})
44+
fmt.Printf("% x\n", "hello")
45+
}
46+
output.StdoutHr()
47+
48+
// 0埋めして出力
49+
{
50+
fmt.Printf("'%08d'\n", 0xf0)
51+
fmt.Printf("'%08x'\n", 0xff)
52+
fmt.Printf("'%08b'\n", 0x0f)
53+
}
54+
55+
return nil
56+
}

0 commit comments

Comments
 (0)