done
This commit is contained in:
41
slownie.go
41
slownie.go
@ -1,12 +1,10 @@
|
||||
package main
|
||||
package slownie
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var _smallNumbers = []string{
|
||||
@ -37,9 +35,19 @@ var _zlotyWord = []string{"złoty", "złote", "złotych"}
|
||||
|
||||
var _groszyWord = []string{"grosz", "grosze", "groszy"}
|
||||
|
||||
func round(num float64) int {
|
||||
return int(num + math.Copysign(0.5, num))
|
||||
}
|
||||
|
||||
func toFixed(num float64, precision int) float64 {
|
||||
output := math.Pow(10, float64(precision))
|
||||
return float64(round(num*output)) / output
|
||||
}
|
||||
|
||||
func split(value float64) (int64, int32) {
|
||||
pre := int64(value)
|
||||
post := int32(math.Mod((value*100 + 0.5), 100))
|
||||
// post := int32((value - float64(pre)) * 100)
|
||||
post := int32(math.Mod((value*100)+0.5, 100))
|
||||
return pre, post
|
||||
}
|
||||
|
||||
@ -116,7 +124,9 @@ func someSlownie(value int64, some []string) string {
|
||||
return slownie(value) + " " + some[caseWord(int(value))]
|
||||
}
|
||||
|
||||
func kwotaSlownie(input float64) (output string, err error) {
|
||||
// KwotaSlownie - return value in words of zloty and groszy
|
||||
func KwotaSlownie(input float64) (output string, err error) {
|
||||
input = toFixed(input, 2)
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err = errors.New("too big number")
|
||||
@ -128,24 +138,5 @@ func kwotaSlownie(input float64) (output string, err error) {
|
||||
input = -input
|
||||
}
|
||||
zloty, grosze := split(input)
|
||||
return minusword + someSlownie(zloty, _zlotyWord) + fmt.Sprintf(" %d/100", grosze), nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
x := r.Float64() * 10000000000000000
|
||||
if kwota, err := kwotaSlownie(x); err == nil {
|
||||
fmt.Printf("%.2f %s\n", x, kwota)
|
||||
} else {
|
||||
fmt.Printf("%.2f %s\n", x, err)
|
||||
}
|
||||
}
|
||||
x := 4831179.50
|
||||
if kwota, err := kwotaSlownie(x); err == nil {
|
||||
fmt.Printf("%.2f %s\n", x, kwota)
|
||||
} else {
|
||||
fmt.Printf("%.2f %s\n", x, err)
|
||||
}
|
||||
return minusword + someSlownie(zloty, _zlotyWord) + fmt.Sprintf(" %02d/100", grosze), nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user