26 lines
786 B
Go
Executable File

package points
import "math"
// CentsToPoints converts monetary value (in cents) to points based on the exchange rate (X Points per 1 Yuan).
// Now: 1 Yuan = 100 Cents.
// If Rate = 1 (1 Point = 1 Yuan), then 100 Cents = 1 Point.
// Formula: points = (cents * rate) / 100
func CentsToPoints(cents int64, rate float64) int64 {
if rate <= 0 {
rate = 1
}
// Use rounding to avoid precision loss on division
return int64(math.Round((float64(cents) * rate) / 100.0))
}
// PointsToCents converts points to monetary value (in cents).
// If Rate = 1 (1 Point = 1 Yuan), then 1 Point = 100 Cents.
// Formula: cents = (points * 100) / rate
func PointsToCents(points int64, rate float64) int64 {
if rate <= 0 {
rate = 1
}
return int64(math.Round((float64(points) * 100.0) / rate))
}