MCPcopy Create free account
hub / github.com/cockroachdb/apd / Format

Method Format

format.go:156–217  ·  view source on GitHub ↗

*Decimal must implement fmt.Formatter Format implements fmt.Formatter. It accepts many of the regular formats for floating-point numbers ('e', 'E', 'f', 'F', 'g', 'G') as well as 's' and 'v', which are handled like 'G'. Format also supports the output field width, as well as the format flags '+' and

(s fmt.State, format rune)

Source from the content-addressed store, hash-verified

154// padding, and '-' for left or right justification. It does not support
155// precision. See the fmt package for details.
156func (d *Decimal) Format(s fmt.State, format rune) {
157 switch format {
158 case 'e', 'E', 'f', 'g', 'G':
159 // nothing to do
160 case 'F':
161 // (*Decimal).Text doesn't support 'F'; handle like 'f'
162 format = 'f'
163 case 'v', 's':
164 // handle like 'G'
165 format = 'G'
166 default:
167 fmt.Fprintf(s, "%%!%c(*apd.Decimal=%s)", format, d.String())
168 return
169 }
170 var buf []byte
171 buf = d.Append(buf, byte(format))
172 if len(buf) == 0 {
173 buf = []byte("?") // should never happen, but don't crash
174 }
175 // len(buf) > 0
176
177 var sign string
178 switch {
179 case buf[0] == '-':
180 sign = "-"
181 buf = buf[1:]
182 case buf[0] == '+':
183 // +Inf
184 sign = "+"
185 if s.Flag(' ') {
186 sign = " "
187 }
188 buf = buf[1:]
189 case s.Flag('+'):
190 sign = "+"
191 case s.Flag(' '):
192 sign = " "
193 }
194
195 var padding int
196 if width, hasWidth := s.Width(); hasWidth && width > len(sign)+len(buf) {
197 padding = width - len(sign) - len(buf)
198 }
199
200 switch {
201 case s.Flag('0') && d.Form == Finite:
202 // 0-padding on left
203 writeMultiple(s, sign, 1)
204 writeMultiple(s, "0", padding)
205 s.Write(buf)
206 case s.Flag('-'):
207 // padding on right
208 writeMultiple(s, sign, 1)
209 s.Write(buf)
210 writeMultiple(s, " ", padding)
211 default:
212 // padding on left
213 writeMultiple(s, " ", padding)

Callers

nothing calls this directly

Calls 3

StringMethod · 0.95
AppendMethod · 0.95
writeMultipleFunction · 0.85

Tested by

no test coverage detected