sprintf( "%0.2f", my_bmi ) : sprintf « Development « Ruby






sprintf( "%0.2f", my_bmi )



#!/usr/bin/env ruby

BEGIN { puts "Date and time: " + Time.now.to_s }

def bmi( weight, height )
  703.0*( weight.to_f/(height.to_f**2))
end

my_bmi = bmi( 196, 73 )

puts "Your BMI is: " + x = sprintf( "%0.2f", my_bmi )

 








Related examples in the same category

1.Field types for sprintf
2.Sprintf Flags and Field Types
3.Use sprintf("%08b", num) to convert input to binary number
4.Print out time in Begin and end block