Skip to content

Commit

Permalink
Format integer floats without .0 suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jan 7, 2025
1 parent 4f1f6e5 commit 2d77203
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion arrow-cast/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,15 @@ macro_rules! primitive_display_float {
{
fn write(&self, idx: usize, f: &mut dyn Write) -> FormatResult {
let value = self.value(idx);

let mut buffer = ryu::Buffer::new();
f.write_str(buffer.format(value))?;
let string = buffer.format(value);

// Remove the `.0` suffix on integers that ryu always adds:
let string = string.strip_suffix(".0").unwrap_or(string);

f.write_str(string)?;

Ok(())
}
})+
Expand Down Expand Up @@ -1219,4 +1226,11 @@ mod tests {
array_value_to_string(&map_array, 3).unwrap()
);
}

#[test]
fn test_float_arry_to_string() {
let array = Float32Array::from(vec![0.0, 1.0, 2.0, 13.37, std::f32::consts::PI]);
let default_strings = format_array(&array, &Default::default());
assert_eq!(default_strings, ["0", "1", "2", "13.37", "3.1415927"]);
}
}

0 comments on commit 2d77203

Please sign in to comment.