Andrew Gerrand | ddd67f2 | 2012-01-25 10:29:44 +1100 | [diff] [blame] | 1 | // Copyright 2012 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package big_test |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "log" |
| 10 | "math/big" |
| 11 | ) |
| 12 | |
Andrew Gerrand | ddd67f2 | 2012-01-25 10:29:44 +1100 | [diff] [blame] | 13 | func ExampleRat_SetString() { |
| 14 | r := new(big.Rat) |
| 15 | r.SetString("355/113") |
| 16 | fmt.Println(r.FloatString(3)) |
Andrew Gerrand | 11e113d | 2012-02-16 11:50:28 +1100 | [diff] [blame] | 17 | // Output: 3.142 |
Andrew Gerrand | ddd67f2 | 2012-01-25 10:29:44 +1100 | [diff] [blame] | 18 | } |
| 19 | |
Andrew Gerrand | ddd67f2 | 2012-01-25 10:29:44 +1100 | [diff] [blame] | 20 | func ExampleInt_SetString() { |
| 21 | i := new(big.Int) |
| 22 | i.SetString("644", 8) // octal |
| 23 | fmt.Println(i) |
Andrew Gerrand | 11e113d | 2012-02-16 11:50:28 +1100 | [diff] [blame] | 24 | // Output: 420 |
Andrew Gerrand | ddd67f2 | 2012-01-25 10:29:44 +1100 | [diff] [blame] | 25 | } |
| 26 | |
Andrew Gerrand | ddd67f2 | 2012-01-25 10:29:44 +1100 | [diff] [blame] | 27 | func ExampleRat_Scan() { |
| 28 | // The Scan function is rarely used directly; |
| 29 | // the fmt package recognizes it as an implementation of fmt.Scanner. |
| 30 | r := new(big.Rat) |
| 31 | _, err := fmt.Sscan("1.5000", r) |
| 32 | if err != nil { |
| 33 | log.Println("error scanning value:", err) |
| 34 | } else { |
| 35 | fmt.Println(r) |
| 36 | } |
Andrew Gerrand | 11e113d | 2012-02-16 11:50:28 +1100 | [diff] [blame] | 37 | // Output: 3/2 |
Andrew Gerrand | ddd67f2 | 2012-01-25 10:29:44 +1100 | [diff] [blame] | 38 | } |
| 39 | |
Andrew Gerrand | ddd67f2 | 2012-01-25 10:29:44 +1100 | [diff] [blame] | 40 | func ExampleInt_Scan() { |
| 41 | // The Scan function is rarely used directly; |
| 42 | // the fmt package recognizes it as an implementation of fmt.Scanner. |
| 43 | i := new(big.Int) |
| 44 | _, err := fmt.Sscan("18446744073709551617", i) |
| 45 | if err != nil { |
| 46 | log.Println("error scanning value:", err) |
| 47 | } else { |
| 48 | fmt.Println(i) |
| 49 | } |
Andrew Gerrand | 11e113d | 2012-02-16 11:50:28 +1100 | [diff] [blame] | 50 | // Output: 18446744073709551617 |
Andrew Gerrand | ddd67f2 | 2012-01-25 10:29:44 +1100 | [diff] [blame] | 51 | } |