86 lines
1.7 KiB
Go
86 lines
1.7 KiB
Go
|
|
package jrzq
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"math"
|
||
|
|
"strconv"
|
||
|
|
)
|
||
|
|
|
||
|
|
// DecodeIntervalMidpoint 将 xyp 区间码解码为区间中位整数。
|
||
|
|
func DecodeIntervalMidpoint(field, code string) int {
|
||
|
|
if code == "" || code == "0" {
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
rules, ok := loanRiskIntervalMappings[field]
|
||
|
|
if !ok {
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
for _, rule := range rules {
|
||
|
|
if rule.output != code {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
min := rule.min
|
||
|
|
max := rule.max
|
||
|
|
if max >= math.MaxFloat64/4 {
|
||
|
|
if rule.minInclusive {
|
||
|
|
return int(math.Round(min + 1))
|
||
|
|
}
|
||
|
|
return int(math.Round(min + 2))
|
||
|
|
}
|
||
|
|
lo := min
|
||
|
|
hi := max
|
||
|
|
if !rule.minInclusive {
|
||
|
|
lo += 0.5
|
||
|
|
}
|
||
|
|
if !rule.maxInclusive {
|
||
|
|
hi -= 0.5
|
||
|
|
}
|
||
|
|
return int(math.Round((lo + hi) / 2))
|
||
|
|
}
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
// FormatLoanRiskInterval 将 xyp 区间码格式化为可读区间字符串。
|
||
|
|
func FormatLoanRiskInterval(field, code string) string {
|
||
|
|
if code == "" || code == "0" {
|
||
|
|
return "0"
|
||
|
|
}
|
||
|
|
rules, ok := loanRiskIntervalMappings[field]
|
||
|
|
if !ok {
|
||
|
|
return "0"
|
||
|
|
}
|
||
|
|
for _, rule := range rules {
|
||
|
|
if rule.output != code {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
return formatIntervalBounds(rule.min, rule.max, rule.minInclusive, rule.maxInclusive)
|
||
|
|
}
|
||
|
|
return "0"
|
||
|
|
}
|
||
|
|
|
||
|
|
func formatIntervalBounds(min, max float64, minInclusive, maxInclusive bool) string {
|
||
|
|
if max >= math.MaxFloat64/4 {
|
||
|
|
left := "("
|
||
|
|
if minInclusive {
|
||
|
|
left = "["
|
||
|
|
}
|
||
|
|
return fmt.Sprintf("%s%s,+)", left, formatBound(min))
|
||
|
|
}
|
||
|
|
left := "("
|
||
|
|
if minInclusive {
|
||
|
|
left = "["
|
||
|
|
}
|
||
|
|
right := ")"
|
||
|
|
if maxInclusive {
|
||
|
|
right = "]"
|
||
|
|
}
|
||
|
|
return fmt.Sprintf("%s%s,%s%s", left, formatBound(min), formatBound(max), right)
|
||
|
|
}
|
||
|
|
|
||
|
|
func formatBound(v float64) string {
|
||
|
|
if v == float64(int64(v)) {
|
||
|
|
return strconv.FormatInt(int64(v), 10)
|
||
|
|
}
|
||
|
|
return strconv.FormatFloat(v, 'f', -1, 64)
|
||
|
|
}
|