Logo

Programming-Idioms

  • Rust
  • JS
  • Java

Idiom #79 Convert integer to floating point number

Declare the floating point number y and initialize it with the value of the integer x .

float y = x;
int x = 10
float y = (float)x;
System.out.println(y);
import java.math.BigDecimal;
float y = new BigDecimal(x).floatValue();
Float y = Float.parseFloat(String.valueOf(x));
let y = x as f64;
let y = x + .0

You might say "Wait! All JavaScript numbers are floats!"
They certainly always behave like they are on the outside, but on the inside they treat some numbers as integers.
Y : constant Float := Float (X);

New implementation...