MCPcopy Create free account
hub / github.com/apache/datafusion / round_float

Function round_float

datafusion/spark/src/function/math/round.rs:189–205  ·  view source on GitHub ↗

Round a floating-point value to the given number of decimal places using HALF_UP rounding mode (ties round away from zero). This matches Spark's `RoundBase` behaviour for `FloatType` / `DoubleType`, which internally converts the value to `BigDecimal` and rounds with `RoundingMode.HALF_UP`. # Arguments `value` – the floating-point number to round `scale` – number of decimal places to keep. - `sca

(value: T, scale: i32)

Source from the content-addressed store, hash-verified

187/// round_float(125.0, -1) → 130.0
188/// ```
189fn round_float<T: num_traits::Float>(value: T, scale: i32) -> T {
190 if scale >= 0 {
191 let factor = T::from(10.0f64.powi(scale)).unwrap_or_else(T::infinity);
192 if factor.is_infinite() {
193 // Very large positive scale — value is already precise enough, return as-is
194 return value;
195 }
196 (value * factor).round() / factor
197 } else {
198 let factor = T::from(10.0f64.powi(-scale)).unwrap_or_else(T::infinity);
199 if factor.is_infinite() {
200 // Very large negative scale — any finite value rounds to 0
201 return T::zero();
202 }
203 (value / factor).round() * factor
204 }
205}
206
207/// Round an integer value to the given scale using HALF_UP rounding mode.
208///

Callers 1

spark_roundFunction · 0.70

Calls 1

is_infiniteMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…