Functional Weave
Code in Rust

charts.format@1.0.0

impl/rust/format_money_tick.rs

686 bytes · the Rust implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one
use super::math_round_float::round_float;  ← from math.round-float ^1.0.0 · built alongside by fune
use super::money_amount::money;  ← from money.amount ^1.0.0 · built alongside by fune
use super::money_format::format_money;  ← from money.format ^1.0.0 · built alongside by fune

/// A money-axis tick: rounded half away from zero to a whole minor unit, then
/// printed by money.format.
///
/// # Panics
/// Panics if `minor` is not finite or the currency is unknown.
pub fn format_money_tick(minor: f64, currency: &str) -> String {
    if !minor.is_finite() {
        panic!("minor must be a finite number, received {}", minor);
    }
    format_money(&money(round_float(minor, 0) as i64, currency))
}

pub fn fune_vector(args: &[Value]) -> Value {
    Value::str(&format_money_tick(args[0].as_f64(), args[1].as_str()))
}