Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

math/big : Printf %s #24168

Closed
Konstantin8105 opened this issue Feb 28, 2018 · 2 comments
Closed

math/big : Printf %s #24168

Konstantin8105 opened this issue Feb 28, 2018 · 2 comments

Comments

@Konstantin8105
Copy link
Contributor

What version of Go are you using (go version)?

go playground and go 1.8.3 linux/amd64

Does this issue reproduce with the latest release?

checked only for go playground and go 1.8.3 linux/amd64

What operating system and processor architecture are you using (go env)?

ubuntu linux x64

What did you do?

Playground:
https://play.golang.org/p/VNajWTsFQRd

Code:

package main

import (
	"fmt"
	"math/big"
)

type Val struct {
	a int
	b float64
}

func (v Val) String() string {
	return fmt.Sprintf("Val{%d,%v}", v.a, v.b)
}

func main() {
	// user struct
	vl := Val{12, 45}
	fmt.Printf("User struct => %s\n", vl)

	// big float
	f := big.NewFloat(23)
	fmt.Printf("Big Float 1 => %s\n", f)
	fmt.Printf("Big Float 2 => %s\n", (*f))
	fmt.Printf("Big Float 3 => %s\n", f.String())
	fmt.Printf("Big Float 4 => %s\n", (*f).String())
	fmt.Println("Big Float 5 => ", f)
}

Expected behavior:

  1. struct big.Float implemented interface Stringer from package fmt.
  2. In according to doc package fmt:
  1. If an operand implements method String() string, that method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any).

What did you expect to see?

User struct => Val{12,45}
Big Float 1 => 23
Big Float 2 => 23
Big Float 3 => 23
Big Float 4 => 23
Big Float 5 =>  23

What did you see instead?

Result from golang platground:

User struct => Val{12,45}
Big Float 1 => %!s(*big.Float=23)
Big Float 2 => {%!s(uint32=53) %!s(big.RoundingMode=0) %!s(big.Accuracy=0) %!s(big.form=1) %!s(bool=false) [%!s(big.Word=0) %!s(big.Word=3087007744)] %!s(int32=5)}
Big Float 3 => 23
Big Float 4 => 23
Big Float 5 =>  23
@AlexRouSg
Copy link
Contributor

https://golang.org/pkg/math/big/#Float.String

String formats x like x.Text('g', 10). (String must be called explicitly, Float.Format does not support %s verb.)

@Konstantin8105
Copy link
Contributor Author

Thank you for clarification. Close this issue

@golang golang locked and limited conversation to collaborators Feb 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants