The trunc_f64_s numeric instruction removes the fractional part of an f64 value and outputs it as a signed integer.
This is a separate instruction, trunc, which removes the fractional part of a float and outputs a float.
There are also other truncate integer conversion instructions:
(module
(import "console" "log" (func $log (param i32)))
(func $main
f64.const 2300044.4 ;; load a number onto the stack
i32.trunc_f64_s ;; discard decimal part and return signed integer
call $log ;; log the result
)
(start $main)
)const url = "{%wasm-url%}";
await WebAssembly.instantiateStreaming(fetch(url), { console });value_type.trunc_f64_s
value_typeThe type of value the instruction is being run on. The following types support trunc_f64_s:
i32i64trunc_f64_sThe trunc_f64_s instruction. Must always be included after the value_type and a period (.).
[input] -> [output]
inputThe input floating point number.
outputThe output integer.
| Instruction | Binary format | Example text => binary |
|---|---|---|
i32.trunc_f64_s | 0xaa | i32.trunc_f64_s => 0xaa |
i64.trunc_f64_s | 0xb0 | i64.trunc_f64_s => 0xb0 |