...

Source file src/gopkg.in/inf.v0/example_test.go

Documentation: gopkg.in/inf.v0

     1  package inf_test
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  )
     7  
     8  import "gopkg.in/inf.v0"
     9  
    10  func ExampleDec_SetString() {
    11  	d := new(inf.Dec)
    12  	d.SetString("012345.67890") // decimal; leading 0 ignored; trailing 0 kept
    13  	fmt.Println(d)
    14  	// Output: 12345.67890
    15  }
    16  
    17  func ExampleDec_Scan() {
    18  	// The Scan function is rarely used directly;
    19  	// the fmt package recognizes it as an implementation of fmt.Scanner.
    20  	d := new(inf.Dec)
    21  	_, err := fmt.Sscan("184467440.73709551617", d)
    22  	if err != nil {
    23  		log.Println("error scanning value:", err)
    24  	} else {
    25  		fmt.Println(d)
    26  	}
    27  	// Output: 184467440.73709551617
    28  }
    29  
    30  func ExampleDec_QuoRound_scale2RoundDown() {
    31  	// 10 / 3 is an infinite decimal; it has no exact Dec representation
    32  	x, y := inf.NewDec(10, 0), inf.NewDec(3, 0)
    33  	// use 2 digits beyond the decimal point, round towards 0
    34  	z := new(inf.Dec).QuoRound(x, y, 2, inf.RoundDown)
    35  	fmt.Println(z)
    36  	// Output: 3.33
    37  }
    38  
    39  func ExampleDec_QuoRound_scale2RoundCeil() {
    40  	// -42 / 400 is an finite decimal with 3 digits beyond the decimal point
    41  	x, y := inf.NewDec(-42, 0), inf.NewDec(400, 0)
    42  	// use 2 digits beyond decimal point, round towards positive infinity
    43  	z := new(inf.Dec).QuoRound(x, y, 2, inf.RoundCeil)
    44  	fmt.Println(z)
    45  	// Output: -0.10
    46  }
    47  
    48  func ExampleDec_QuoExact_ok() {
    49  	// 1 / 25 is a finite decimal; it has exact Dec representation
    50  	x, y := inf.NewDec(1, 0), inf.NewDec(25, 0)
    51  	z := new(inf.Dec).QuoExact(x, y)
    52  	fmt.Println(z)
    53  	// Output: 0.04
    54  }
    55  
    56  func ExampleDec_QuoExact_fail() {
    57  	// 1 / 3 is an infinite decimal; it has no exact Dec representation
    58  	x, y := inf.NewDec(1, 0), inf.NewDec(3, 0)
    59  	z := new(inf.Dec).QuoExact(x, y)
    60  	fmt.Println(z)
    61  	// Output: <nil>
    62  }
    63  

View as plain text