Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

round filter: perform cast to i128 if precision is 0 #899

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/builtins/filters/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,24 @@ pub fn round(value: &Value, args: &HashMap<String, Value>) -> Result<Value> {
};
let multiplier = if precision == 0 { 1.0 } else { 10.0_f64.powi(precision) };

match method.as_ref() {
"common" => Ok(to_value((multiplier * num).round() / multiplier).unwrap()),
"ceil" => Ok(to_value((multiplier * num).ceil() / multiplier).unwrap()),
"floor" => Ok(to_value((multiplier * num).floor() / multiplier).unwrap()),
_ => Err(Error::msg(format!(
"Filter `round` received an incorrect value for arg `method`: got `{:?}`, \
let intermediate_result = match method.as_ref() {
"common" => (multiplier * num).round() / multiplier,
"ceil" => (multiplier * num).ceil() / multiplier,
"floor" => (multiplier * num).floor() / multiplier,
_ => {
return Err(Error::msg(format!(
"Filter `round` received an incorrect value for arg `method`: got `{:?}`, \
only common, ceil and floor are allowed",
method
))),
method
)))
}
};

// Do we need to return an integer or a float?
if precision == 0 {
Ok(to_value(intermediate_result as i128).unwrap())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't i128 in serde_json converted to a string?

} else {
Ok(to_value(intermediate_result).unwrap())
}
}

Expand Down Expand Up @@ -165,7 +174,7 @@ mod tests {
fn test_round_default() {
let result = round(&to_value(2.1).unwrap(), &HashMap::new());
assert!(result.is_ok());
assert_eq!(result.unwrap(), to_value(2.0).unwrap());
assert_eq!(result.unwrap(), to_value(2).unwrap());
}

#[test]
Expand All @@ -183,7 +192,7 @@ mod tests {
args.insert("method".to_string(), to_value("ceil").unwrap());
let result = round(&to_value(2.1).unwrap(), &args);
assert!(result.is_ok());
assert_eq!(result.unwrap(), to_value(3.0).unwrap());
assert_eq!(result.unwrap(), to_value(3).unwrap());
}

#[test]
Expand All @@ -202,7 +211,7 @@ mod tests {
args.insert("method".to_string(), to_value("floor").unwrap());
let result = round(&to_value(2.1).unwrap(), &args);
assert!(result.is_ok());
assert_eq!(result.unwrap(), to_value(2.0).unwrap());
assert_eq!(result.unwrap(), to_value(2).unwrap());
}

#[test]
Expand Down
Loading