Announcement

Collapse
No announcement yet.

Java Trouble --> Float to Int

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Java Trouble --> Float to Int

    Hi guys,

    i need to convert a float value to an integer (basically i want to have a final value with no decimal places). I will need this value to allways be rounded down, i.e.

    12.34 = 12
    43.98 = 43
    0.12 = 0
    ect....... (all rounded down)

    How would i do this in Java? I'm using JBuilder but i'm having really bad problems with it, especially considering sometimes i get a value without decimal places.

    If anyone could help i would be much appreciative.

    Thanks in advance

  • #2
    Originally posted by FunkyChicken
    i need to convert a float value to an integer (basically i want to have a final value with no decimal places). I will need this value to allways be rounded down
    Why not just cast the float/double into an int?

    Originally posted by FunkyChicken
    especially considering sometimes i get a value without decimal places.
    Yes, I believe that is usually the case with integers...

    Comment


    • #3
      im not sure on what this "cast" means, i guess i will have to try and look in up somwhere, many thanks.

      P.s. If i cant find a resolution will you be able to help a little more by giving me some serach terms, thankyou

      cheers

      Comment


      • #4
        just so you guys know i aint being lazy, i have tried these 2 but both fail:


        Code:
        float LeftOver = 3.567;
        int test = LeftOver.intValue();
        and

        Code:
        float LeftOver = 3.567;
        int test = LeftOver;
        neither work!

        Comment


        • #5
          Code:
          int   TestInteger;
          float TestFloat = 3.8644;
          
          TestInteger = (int) TestFloat;
          string Output = "" + TestInteger;


          at last it bloody works, the good thing about it is that it simply chops off the decimal places rather than rounding up or down.

          Comment


          • #6
            round()......
            --- The fuck? Have you ever BEEN to Defcon?

            Comment

            Working...
            X