Skip to content

Commit 5233c2b

Browse files
Address Copilot review: use string manipulation for FormatNumber
Removes float64 parsing to preserve precision for DECIMAL(38,s) values. FormatNumber now uses pure string manipulation with addThousandSeparators and getDecimalSeparator, avoiding any potential precision loss.
1 parent a046229 commit 5233c2b

1 file changed

Lines changed: 6 additions & 15 deletions

File tree

pkg/sqlcmd/regional.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,25 @@ func (r *RegionalSettings) IsEnabled() bool {
4040
}
4141

4242
// FormatNumber formats a numeric value with locale-specific thousand separators
43-
// Used for DECIMAL and NUMERIC types
43+
// Used for DECIMAL and NUMERIC types. Formatting is done purely by string
44+
// manipulation to preserve all digits of high-precision values.
4445
func (r *RegionalSettings) FormatNumber(value string) string {
4546
if !r.enabled || value == "" || value == "NULL" {
4647
return value
4748
}
4849

49-
// Parse the number to get parts
50+
// Handle leading sign
5051
negative := strings.HasPrefix(value, "-")
5152
if negative {
5253
value = value[1:]
5354
}
5455

55-
// Split into integer and decimal parts
56+
// Split into integer and decimal parts using the SQL-style decimal point.
57+
// We do not change any digits; we only insert locale-specific separators.
5658
parts := strings.SplitN(value, ".", 2)
5759
intPart := parts[0]
5860

59-
// Try to parse as float to use the message printer
60-
if f, err := strconv.ParseFloat(strings.Replace(value, ",", "", -1), 64); err == nil {
61-
// Use the message printer with the number formatter for grouping
62-
formatted := r.printer.Sprint(number.Decimal(f))
63-
if negative && !strings.HasPrefix(formatted, "-") {
64-
formatted = "-" + formatted
65-
}
66-
return formatted
67-
}
68-
69-
// Fallback for very large numbers that don't fit in float64
70-
// Add thousand separators manually using locale convention
61+
// Add thousand separators using locale convention (pure string manipulation)
7162
formatted := addThousandSeparators(intPart, r.tag)
7263
if len(parts) > 1 {
7364
formatted += getDecimalSeparator(r.tag) + parts[1]

0 commit comments

Comments
 (0)