{"record":{"id":"2ba4f772eed330f8","repo":"BoundaryML/baml","slug":"to-fixed-method-only-available-on-floats-and-ints-at","errorCode":null,"errorMessage":"to_fixed() method only available on floats and ints at {:?}","messagePattern":"to_fixed\\(\\) method only available on floats and ints at (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2996,"sourceCode":"            }\n            let BamlValueWithMeta::String(search, _) = &args[0] else {\n                bail!(\"replace() search argument must be a string at {:?}\", meta.0);\n            };\n            let BamlValueWithMeta::String(replacement, _) = &args[1] else {\n                bail!(\n                    \"replace() replacement argument must be a string at {:?}\",\n                    meta.0\n                );\n            };\n            // Replace first occurrence only (matching JavaScript behavior)\n            let result = s.replacen(search.as_str(), replacement.as_str(), 1);\n            Ok(BamlValueWithMeta::String(result, meta.clone()))\n        }\n        \"to_fixed\" => {\n            let value = match receiver {\n                BamlValueWithMeta::Float(v, _) => *v,\n                BamlValueWithMeta::Int(v, _) => *v as f64,\n                _ => bail!(\n                    \"to_fixed() method only available on floats and ints at {:?}\",\n                    meta.0\n                ),\n            };\n\n            if args.len() > 1 {\n                bail!(\"to_fixed() method takes at most 1 argument at {:?}\", meta.0);\n            }\n\n            let digits = match args.first() {\n                Some(BamlValueWithMeta::Int(v, _)) => *v,\n                Some(_) => bail!(\"to_fixed() digits argument must be an int at {:?}\", meta.0),\n                None => 0,\n            };\n\n            let formatted = baml_vm::native::number_to_fixed(value, digits)\n                .map_err(|msg| anyhow::anyhow!(\"{msg} at {:?}\", meta.0))?;\n            Ok(BamlValueWithMeta::String(formatted, meta.clone()))","sourceCodeStart":2978,"sourceCodeEnd":3014,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2978-L3014","documentation":"The \"to_fixed\" method formats a number to fixed decimal places and is only defined on Float and Int receivers. The interpreter bails when the receiver is any other value type, attaching the source span. Ints are accepted and widened to f64.","triggerScenarios":"Calling .to_fixed(...) on a string, bool, null, map, array, or media value — commonly when the receiver is a numeric-looking string like \"3.14159\" from an LLM response.","commonSituations":"LLM returns numbers as strings in JSON mode; developer assumes the value is numeric and calls to_fixed on it; iterating heterogeneous arrays and formatting every element.","solutions":["Ensure the receiver is a number: parse the string to a float/int first if it is numeric text","Annotate the variable as float/int so the typechecker enforces it","Guard against null by coalescing to a default number before formatting"],"exampleFix":"// before (value is a string)\nlet out = price.to_fixed(2);\n// after\nlet out = price.ToFloat().to_fixed(2);","handlingStrategy":"type-guard","validationCode":"// receiver must be number: if (v is int || v is float) { v.to_fixed(2); }","typeGuard":"fn is_number(v: Value) -> bool { matches!(v, Value::Int(_) | Value::Float(_)) }","tryCatchPattern":"try { out = v.to_fixed(2); } catch (e) { out = null; log(e); }","preventionTips":["Parse numeric strings before formatting","Type LLM numeric outputs as float/int in the schema","Guard nulls with defaults before calling"],"tags":["runtime","type-mismatch","numbers","baml"],"backgroundTag":"type-mismatch","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}