Thread: How i can convert int->float float->int safely?

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    46

    How i can convert int->float float->int safely?

    How i can convert int->float float->int safely?
    int->float conversion i need accurate way.
    float->int i need both ways on which cuts decimals,and another which makes rounding.
    ps. some os functions here where i going code want int type values. some my routines needs float values etc

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    If you limit the range of integers to +/-[0, 2^24) (largest value < 2^24), then int->float->int should be ok. If you limit the range to +/-[0, 2^53) (largest value < 2^53), then int->double->int should be ok.

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    To convert float to int with truncation of the decimal you dont need anything special, just cast or assign the float to an int variable.

    For rounding you need to manually round or use printf(), sprintf, or math.h round() functions and then cast or assign to int as above

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert float to int?
    By kryonik in forum C++ Programming
    Replies: 11
    Last Post: 09-14-2006, 05:14 PM
  2. help in convert String to float
    By kahwai1984 in forum C++ Programming
    Replies: 7
    Last Post: 03-19-2006, 09:21 PM
  3. Replies: 8
    Last Post: 07-08-2005, 09:12 AM
  4. How to convert from float to int?
    By bjdea1 in forum C Programming
    Replies: 6
    Last Post: 06-11-2003, 03:54 PM
  5. Unresolved external 'convert(float, float)'
    By Ipsec Espah in forum C++ Programming
    Replies: 4
    Last Post: 05-21-2003, 10:08 AM