mirror of
https://github.com/golang/go
synced 2024-11-22 04:14:42 -07:00
math/big: add examples for Rat and Int's SetString and Scan methods
R=golang-dev, bradfitz, rsc, r, gri, r CC=golang-dev https://golang.org/cl/5543047
This commit is contained in:
parent
e3ab30bbc1
commit
ddd67f2ecd
51
src/pkg/math/big/example_test.go
Normal file
51
src/pkg/math/big/example_test.go
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
// Copyright 2012 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package big_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"math/big"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 3.142
|
||||||
|
func ExampleRat_SetString() {
|
||||||
|
r := new(big.Rat)
|
||||||
|
r.SetString("355/113")
|
||||||
|
fmt.Println(r.FloatString(3))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 420
|
||||||
|
func ExampleInt_SetString() {
|
||||||
|
i := new(big.Int)
|
||||||
|
i.SetString("644", 8) // octal
|
||||||
|
fmt.Println(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3/2
|
||||||
|
func ExampleRat_Scan() {
|
||||||
|
// The Scan function is rarely used directly;
|
||||||
|
// the fmt package recognizes it as an implementation of fmt.Scanner.
|
||||||
|
r := new(big.Rat)
|
||||||
|
_, err := fmt.Sscan("1.5000", r)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("error scanning value:", err)
|
||||||
|
} else {
|
||||||
|
fmt.Println(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 18446744073709551617
|
||||||
|
func ExampleInt_Scan() {
|
||||||
|
// The Scan function is rarely used directly;
|
||||||
|
// the fmt package recognizes it as an implementation of fmt.Scanner.
|
||||||
|
i := new(big.Int)
|
||||||
|
_, err := fmt.Sscan("18446744073709551617", i)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("error scanning value:", err)
|
||||||
|
} else {
|
||||||
|
fmt.Println(i)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user