diff --git a/README.md b/README.md index b7a7b18..f3a535b 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,22 @@ import "github.com/duke-git/lancet/function" - [Delay](https://github.com/duke-git/lancet/blob/main/docs/function.md#Delay) - [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function.md#Watcher) + +### Mathutil package implements some functions for math calculation. + +```go +import "github.com/duke-git/lancet/mathutil" +``` + +#### Function list: +- [Exponent](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Exponent) +- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Fibonacci) +- [Percent](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Percent) +- [RoundToFloat](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToFloat) +- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToString) +- [TruncRound](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#TruncRound) + + ### Netutil package contains functions to get net information and send http request. ```go diff --git a/README_zh-CN.md b/README_zh-CN.md index 426c0b2..f895b40 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -195,6 +195,19 @@ import "github.com/duke-git/lancet/function" - [Delay](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Delay) - [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher) +### mathutil包实现了一些数学计算的函数。 + +```go +import "github.com/duke-git/lancet/mathutil" +``` + +#### Function list: +- [Exponent](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Exponent) +- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Fibonacci) +- [Percent](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Percent) +- [RoundToFloat](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToFloat) +- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToString) +- [TruncRound](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#TruncRound) ### netutil网络包支持获取ip地址,发送http请求。 diff --git a/docs/mathutil.md b/docs/mathutil.md new file mode 100644 index 0000000..a10c7f4 --- /dev/null +++ b/docs/mathutil.md @@ -0,0 +1,232 @@ +# Mathutil +Package mathutil implements some functions for math calculation. + +
+ +## Source: + +[https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go) + + + + +## Example: +```go +import ( + "github.com/duke-git/lancet/mathutil" +) +``` + + + +## Index +- [Exponent](#Exponent) +- [Fibonacci](#Fibonacci) +- [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 +} +``` + + + diff --git a/docs/mathutil_zh-CN.md b/docs/mathutil_zh-CN.md new file mode 100644 index 0000000..11b9f95 --- /dev/null +++ b/docs/mathutil_zh-CN.md @@ -0,0 +1,232 @@ +# Mathutil +mathutil包实现了一些数学计算的函数. + + + +## 源码: + +[https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go) + + + + +## 用法: +```go +import ( + "github.com/duke-git/lancet/mathutil" +) +``` + + + +## 目录 +- [Exponent](#Exponent) +- [Fibonacci](#Fibonacci) +- [Percent](#Percent) +- [RoundToFloat](#RoundToFloat) +- [RoundToString](#RoundToString) +- [TruncRound](#TruncRound) + + + +## Documentation + + +### Exponent +指数计算(x的n次方)
+ +函数签名: + +```go +func Exponent(x, n int64) int64 +``` +例子: + +```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 +计算斐波那契数列的第n个数
+ +函数签名: + +```go +func Fibonacci(first, second, n int) int +``` +例子: + +```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 +计算阶乘
+ +函数签名: + +```go +func Factorial(x uint) uint +``` +例子: + +```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 +计算百分比,保留n位小数
+ +函数签名: + +```go +func Percent(val, total float64, n int) float64 +``` +例子: + +```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 +四舍五入,保留n位小数
+ +函数签名: + +```go +func RoundToFloat(x float64, n int) float64 +``` +例子: + +```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 +四舍五入,保留n位小数,返回字符串
+ +函数签名: + +```go +func RoundToString(x float64, n int) string +``` +例子: + +```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 +截短n位小数(不进行四舍五入)
+ +函数签名: + +```go +func TruncRound(x float64, n int) float64 +``` +例子: + +```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 +} +``` + + +