decimal_to_precision

Function decimal_to_precision 

Source
pub fn decimal_to_precision(
    value: Decimal,
    rounding_mode: RoundingMode,
    num_precision_digits: i32,
    counting_mode: Option<CountingMode>,
    padding_mode: Option<PaddingMode>,
) -> Result<String>
Expand description

Formats a decimal to a specific precision with configurable rounding and counting modes.

Core precision formatting function equivalent to Go’s DecimalToPrecision method.

§Arguments

  • value - The decimal value to format
  • rounding_mode - Rounding behavior to apply
  • num_precision_digits - Number of precision digits
  • counting_mode - How to count precision (default: DecimalPlaces)
  • padding_mode - Output padding behavior (default: NoPadding)

§Examples

use ccxt_core::precision::{decimal_to_precision, RoundingMode, CountingMode, PaddingMode};
use rust_decimal::Decimal;
use std::str::FromStr;

let num = Decimal::from_str("123.456789").unwrap();

// 保留2位小数,四舍五入
let result = decimal_to_precision(
    num,
    RoundingMode::Round,
    2,
    Some(CountingMode::DecimalPlaces),
    Some(PaddingMode::NoPadding),
).unwrap();
assert_eq!(result, "123.46");

// 截断到2位小数
let result = decimal_to_precision(
    num,
    RoundingMode::RoundDown,
    2,
    Some(CountingMode::DecimalPlaces),
    Some(PaddingMode::NoPadding),
).unwrap();
assert_eq!(result, "123.45");