When converting from a floating point number to an integer there is
likely to be a loss of precision - for example 3.1 will become 3.
When converting from an integer to a floating point there is no such danger.
1. [ Java ] integer to floating point
Not much can go wrong. The conversion is automatic.
However notice:
- the calculation i/10 is performed as an integer
calculation and gives 123.0
- the calculation i/10.0 is performed as a floating point calculation
and gives 123.4
test text
1. [ C# ] integer to floating point
Not much can go wrong. The conversion is automatic.
However notice:
- the calculation i/10 is performed as an integer
calculation and gives 123.0
- the calculation i/10.0 is performed as a floating point calculation
and gives 123.4
test text
2. [ Java ] Rounding to an integer
We can use Math.round to convert a double to a long or a float to
an int.
We can recast explicitly and have the fractional part of the number
discarded.
test text
2. [ C# ] Rounding to an integer
We can use Math.Round to round up or down then cast
to a long
We can simply cast explicitly and have the fractional part of the number
discarded.
test text