Format implements the NodeFormatter interface.
(ctx *FmtCtx)
| 854 | |
| 855 | // Format implements the NodeFormatter interface. |
| 856 | func (d *DFloat) Format(ctx *FmtCtx) { |
| 857 | fl := float64(*d) |
| 858 | |
| 859 | disambiguate := ctx.flags.HasFlags(fmtDisambiguateDatumTypes) |
| 860 | parsable := ctx.flags.HasFlags(FmtParsableNumerics) |
| 861 | quote := parsable && (math.IsNaN(fl) || math.IsInf(fl, 0)) |
| 862 | // We need to use Signbit here and not just fl < 0 because of -0. |
| 863 | needParens := !quote && (disambiguate || parsable) && math.Signbit(fl) |
| 864 | // If the number is negative, we need to use parens or the `:::INT` type hint |
| 865 | // will take precedence over the negation sign. |
| 866 | if quote { |
| 867 | ctx.WriteByte('\'') |
| 868 | } else if needParens { |
| 869 | ctx.WriteByte('(') |
| 870 | } |
| 871 | if _, frac := math.Modf(fl); frac == 0 && -1000000 < *d && *d < 1000000 { |
| 872 | // d is a small whole number. Ensure it is printed using a decimal point. |
| 873 | ctx.Printf("%.1f", fl) |
| 874 | } else { |
| 875 | ctx.Printf("%g", fl) |
| 876 | } |
| 877 | if quote { |
| 878 | ctx.WriteByte('\'') |
| 879 | } else if needParens { |
| 880 | ctx.WriteByte(')') |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | // Size implements the Datum interface. |
| 885 | func (d *DFloat) Size() uintptr { |