Skip to content

Commit 38ac3ee

Browse files
Improve float formatting to also handle SMALLMONEY as 32-bit
- Add SMALLMONEY to the list of types that use 32-bit precision - Enhance comment to explain bitSize usage more clearly Co-authored-by: dlevy-msft-sql <194277063+dlevy-msft-sql@users.noreply.github.com>
1 parent 635cb07 commit 38ac3ee

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

pkg/sqlcmd/format.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,13 +535,15 @@ func (f *sqlCmdFormatterType) scanRow(rows *sql.Rows) ([]string, error) {
535535
// Format float64 to match ODBC sqlcmd behavior
536536
// Use 'f' format with -1 precision to avoid scientific notation for typical values
537537
// Fall back to 'g' format if the result would exceed the column display width
538-
538+
539539
// Use appropriate bitSize based on the SQL type (REAL=32, FLOAT=64)
540+
// REAL columns should use 32-bit precision even though the value is scanned as float64
540541
bitSize := 64
541-
if f.columnDetails[n].col.DatabaseTypeName() == "REAL" {
542+
typeName := f.columnDetails[n].col.DatabaseTypeName()
543+
if typeName == "REAL" || typeName == "SMALLMONEY" {
542544
bitSize = 32
543545
}
544-
546+
545547
formatted := strconv.FormatFloat(x, 'f', -1, bitSize)
546548
displayWidth := f.columnDetails[n].displayWidth
547549
if displayWidth > 0 && int64(len(formatted)) > displayWidth {
@@ -552,7 +554,7 @@ func (f *sqlCmdFormatterType) scanRow(rows *sql.Rows) ([]string, error) {
552554
case float32:
553555
// Format float32 to match ODBC sqlcmd behavior
554556
// float32 values are rare (database/sql typically normalizes to float64)
555-
// but handle them if they occur
557+
// Use bitSize 32 to maintain precision appropriate for the original float32 value
556558
formatted := strconv.FormatFloat(float64(x), 'f', -1, 32)
557559
displayWidth := f.columnDetails[n].displayWidth
558560
if displayWidth > 0 && int64(len(formatted)) > displayWidth {

0 commit comments

Comments
 (0)