Skip to content

Commit

Permalink
Don´t print humanized format in single-command mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
DurvalMenezes committed Nov 18, 2024
1 parent afa874b commit 8fb6f25
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func stripTrailingDigits(s string, digits int) string {

// formatNumber formats the number using base and decimals. For bases different
// than 10, non-integer floating numbers are truncated.
func formatNumber(ctx decimal.Context, n *decimal.Big, base, decimals int) string {
func formatNumber(ctx decimal.Context, n *decimal.Big, base, decimals int, single bool) string {
// Print NaN without suffix numbers.
if n.IsNaN(0) {
return strings.TrimRight(fmt.Sprint(n), "0123456789")
Expand Down Expand Up @@ -134,8 +134,8 @@ func formatNumber(ctx decimal.Context, n *decimal.Big, base, decimals int) strin
buf.WriteString(fmt.Sprintf("0x%x%s", n64, suffix))
default:
h := commafWithDigits(n, decimals)
// Only print humanized format when it differs from original value.
if h != clean {
// Only print humanized format when it differs from original value, and not in single-command mode
if h != clean && !single {
suffix = " (" + h + ")"
}
buf.WriteString(clean + suffix)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func calc(stack *stackType, cmd string) error {

if autoprint {
if single {
fmt.Println(stack.top()) // plain print to stdout
fmt.Println(formatNumber(ctx, stack.top(), ops.base, ops.decimals, true)) // plain print to stdout
} else {
stack.printTop(ctx, ops.base, ops.decimals) // pretty print to terminal
}
Expand Down
4 changes: 2 additions & 2 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (x *stackType) top() *decimal.Big {

// printTop displays the top of the stack using the base indicated.
func (x *stackType) printTop(ctx decimal.Context, base, decimals int) {
color.Cyan("= %s", formatNumber(ctx, x.top(), base, decimals))
color.Cyan("= %s", formatNumber(ctx, x.top(), base, decimals, false))
}

// print displays the contents of the stack using the base indicated.
Expand All @@ -67,6 +67,6 @@ func (x *stackType) print(ctx decimal.Context, base, decimals int) {
case last - 1:
tag = " y"
}
fmt.Printf("%s: %s\n", tag, formatNumber(ctx, x.list[ix], base, decimals))
fmt.Printf("%s: %s\n", tag, formatNumber(ctx, x.list[ix], base, decimals, false))
}
}

0 comments on commit 8fb6f25

Please # to comment.