;================================================================
; common/common_utils 通用工具函数
; utils_math.erb 数学运算相关
;================================================================


;;;保留小数的除法运算
;;;- int dividend 被除数
;;;- int divisor 除数
;;;- int decimal_bit 保留小数位数
;;;@return 返回带小数的商(字符串形式)
@div(dividend, divisor, decimal_bit=0)
#FUNCTIONS
    #DIM dividend
    #DIM divisor
    #DIM decimal_bit
    #DIM DYNAMIC scale
    #DIM DYNAMIC quotient_integer
    #DIM DYNAMIC quotient_fractional
    #DIMS DYNAMIC quotient
    scale = POWER(10, decimal_bit)
    dividend = dividend * 10 * scale
    quotient_integer = (dividend / divisor) / (10 * scale)
    quotient_fractional = (dividend / divisor) % (10 * scale)
    IF decimal_bit > 0
        quotient '= @"{quotient_integer}.%TOSTR(quotient_fractional / 10 + (quotient_fractional % 10 >= 5 ? 1 # 0), @"D{decimal_bit}")%"
    ELSE
        quotient '= @"{quotient_integer + (quotient_fractional >= 5 ? 1 # 0)}"
    ENDIF
    RETURNF quotient
