Merge 'printf should truncates floats' from Pavan Nambi

closes https://github.com/tursodatabase/turso/issues/3308

Reviewed-by: Preston Thorpe <preston@turso.tech>

Closes #3415
This commit is contained in:
Preston Thorpe
2025-10-01 19:31:39 -04:00
committed by GitHub
2 changed files with 14 additions and 2 deletions

View File

@@ -66,8 +66,11 @@ pub fn exec_printf(values: &[Register]) -> crate::Result<Value> {
}
let value = &values[args_index].get_value();
match value {
Value::Integer(_) => result.push_str(&format!("{value}")),
Value::Float(_) => result.push_str(&format!("{value}")),
Value::Integer(i) => result.push_str(&i.to_string()),
Value::Float(f) => {
let truncated_val = *f as i64;
result.push_str(&truncated_val.to_string());
}
_ => result.push('0'),
}
args_index += 1;
@@ -323,6 +326,10 @@ mod tests {
vec![text("Number: %d"), text("not a number")],
text("Number: 0"),
),
(
vec![text("Truncated float: %d"), float(3.9)],
text("Truncated float: 3"),
),
(vec![text("Number: %i"), integer(42)], text("Number: 42")),
];
for (input, output) in test_cases {

View File

@@ -220,3 +220,8 @@ do_execsql_test_error_content select-nested-agg-func {
do_execsql_test_error_content select-nested-agg-func-in-expression {
SELECT CASE WHEN max(abs(sum(age))) > 0 THEN 1 ELSE 0 END, sum(age) FROM users;
} {"misuse of aggregate function"}
# https://github.com/tursodatabase/turso/issues/3308
do_execsql_test printf-19029102 {
select printf('%d', 3.9);
} {3}