diff --git a/core/functions/printf.rs b/core/functions/printf.rs index 8074176a2..69d37a5f5 100644 --- a/core/functions/printf.rs +++ b/core/functions/printf.rs @@ -66,8 +66,11 @@ pub fn exec_printf(values: &[Register]) -> crate::Result { } 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 { diff --git a/testing/agg-functions.test b/testing/agg-functions.test index 52fc16f51..26088578a 100755 --- a/testing/agg-functions.test +++ b/testing/agg-functions.test @@ -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}