# Mathutil Package mathutil implements some functions for math calculation.
## Source: [https://github.com/duke-git/lancet/blob/v1/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/v1/mathutil/mathutil.go)
## Example: ```go import ( "github.com/duke-git/lancet/mathutil" ) ```
## Index - [Exponent](#Exponent) - [Fibonacci](#Fibonacci) - [Factorial](#Factorial) - [Percent](#Percent) - [RoundToFloat](#RoundToFloat) - [RoundToString](#RoundToString) - [TruncRound](#TruncRound)
## Documentation ### Exponent

Calculate x to the nth power.

Signature: ```go func Exponent(x, n int64) int64 ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/mathutil" ) func main() { fmt.Println(mathutil.Exponent(10, 0)) //1 fmt.Println(mathutil.Exponent(10, 1)) //10 fmt.Println(mathutil.Exponent(10, 2)) //100 } ``` ### Fibonacci

Calculate the nth number of fibonacci sequence.

Signature: ```go func Fibonacci(first, second, n int) int ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/mathutil" ) func main() { fmt.Println(mathutil.Fibonacci(1, 1, 1)) //1 fmt.Println(mathutil.Fibonacci(1, 1, 2)) //1 fmt.Println(mathutil.Fibonacci(1, 1, 3)) //2 fmt.Println(mathutil.Fibonacci(1, 1, 4)) //3 fmt.Println(mathutil.Fibonacci(1, 1, 5)) //5 } ``` ### Factorial

Calculate the factorial of x.

Signature: ```go func Factorial(x uint) uint ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/mathutil" ) func main() { fmt.Println(mathutil.Factorial(0)) //1 fmt.Println(mathutil.Factorial(1)) //1 fmt.Println(mathutil.Factorial(2)) //2 fmt.Println(mathutil.Factorial(3)) //6 } ``` ### Percent

calculate the percentage of val to total, retain n decimal places.

Signature: ```go func Percent(val, total float64, n int) float64 ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/mathutil" ) func main() { fmt.Println(mathutil.Percent(1, 2, 2)) //1 fmt.Println(mathutil.Percent(0.1, 0.3, 2)) //33.33 } ``` ### RoundToFloat

Round float up to n decimal places.

Signature: ```go func RoundToFloat(x float64, n int) float64 ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/mathutil" ) func main() { fmt.Println(mathutil.RoundToFloat(0, 0)) //0 fmt.Println(mathutil.RoundToFloat(0, 1)) //0 fmt.Println(mathutil.RoundToFloat(0.124, 2)) //0.12 fmt.Println(mathutil.RoundToFloat(0.125, 2)) //0.13 fmt.Println(mathutil.RoundToFloat(0.125, 3)) //0.125 } ``` ### RoundToString

Round float up to n decimal places. will return string.

Signature: ```go func RoundToString(x float64, n int) string ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/mathutil" ) func main() { fmt.Println(mathutil.RoundToString(0, 0)) //"0" fmt.Println(mathutil.RoundToString(0, 1)) //"0.0: fmt.Println(mathutil.RoundToString(0.124, 2)) //"0.12" fmt.Println(mathutil.RoundToString(0.125, 2)) //"0.13" fmt.Println(mathutil.RoundToString(0.125, 3)) //"0.125" } ``` ### TruncRound

Round float off n decimal places.

Signature: ```go func TruncRound(x float64, n int) float64 ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/mathutil" ) func main() { fmt.Println(mathutil.TruncRound(0, 0)) //0 fmt.Println(mathutil.TruncRound(0, 1)) //0 fmt.Println(mathutil.TruncRound(0.124, 2)) //0.12 fmt.Println(mathutil.TruncRound(0.125, 2)) //0.12 fmt.Println(mathutil.TruncRound(0.125, 3)) //0.125 } ```