Conversion/rounding from Float64 to Int64

I have some C++ code as follows:

const double fm = n*p + p;
const int64_t m = (int64_t) fm;

n is int64_t and p is double. How can I replicate what this code does in Julia, as the following (rightly) throws an InexactError:

fm = n*p + p
m = convert(Int64,fm)

Use trunc(Int64, fm) or if you are absolutely sure it won’t overflow, unsafe_trunc(Int64, fmt).

3 Likes