Function ryu_js::raw::format64_to_fixed
source · pub unsafe fn format64_to_fixed(
f: f64,
fraction_digits: u8,
result: *mut u8
) -> usize
Expand description
Print f64
to the given buffer using fixed notation,
as defined in the ECMAScript Number.prototype.toFixed()
method
and return number of bytes written.
At most 132 bytes will be written.
§Special cases
This function does not check for NaN or infinity. If the input number is not a finite float, the printed representation will be some correctly formatted but unspecified numerical value.
Please check is_finite
yourself before calling this function, or
check is_nan
and is_infinite
and handle those cases yourself.
§Safety
The result
pointer argument must point to sufficiently many writable bytes
to hold Ryū’s representation of f
.
§Example
use std::{mem::MaybeUninit, slice, str};
let f = 1.235f64;
unsafe {
let mut buffer = [MaybeUninit::<u8>::uninit(); 132];
let len = ryu_js::raw::format64_to_fixed(f, 2, buffer.as_mut_ptr() as *mut u8);
let slice = slice::from_raw_parts(buffer.as_ptr() as *const u8, len);
let print = str::from_utf8_unchecked(slice);
assert_eq!(print, "1.24");
}