float to integer

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • clueless
    Forum Member
    • Jun 2006
    • 421

    float to integer

    Does anyone know how to convert a floating point number into an integer value ? thanks.
  • longedge
    Indigo Rose Customer
    • Aug 2003
    • 2498

    #2
    How accurate do you have to be?

    Would -

    Code:
    var_int = Math.Floor(var_mynumber+0.5);
    be near enough?

    Comment

    • clueless
      Forum Member
      • Jun 2006
      • 421

      #3
      Thanks but i managed to make a DLL in C++ that does the job

      Code:
      function GetInt(FloatNumber)
      return DLL.CallFunction("AutoPlay\\Dlls\\Integers.dll", "ReturnInt", FloatNumber, DLL_RETURN_TYPE_INTEGER, DLL_CALL_STDCALL);
      end
      Attached Files

      Comment

      • Desolator
        Forum Member
        • Apr 2007
        • 292

        #4
        Did you make sure that the DLL handles double and not float? Lua interprets numbers as double.

        Comment

        • clueless
          Forum Member
          • Jun 2006
          • 421

          #5
          Im not sure what you mean, to be honest this is the first DLL ive ever made.
          It accepts a Float and returns a Integer.It seems to work ok for me in AMS7.

          Comment

          • Desolator
            Forum Member
            • Apr 2007
            • 292

            #6
            What language did you use to make this? Lua uses `double' instead of `float' for ALL numbers, so if I send it a bigger number I'll get a negative number instead.

            Comment

            • clueless
              Forum Member
              • Jun 2006
              • 421

              #7
              Visual C++

              Comment

              • Desolator
                Forum Member
                • Apr 2007
                • 292

                #8
                Then mind replacing `float' with `double'?

                Comment

                • clueless
                  Forum Member
                  • Jun 2006
                  • 421

                  #9
                  It will probably work ok with a double to.

                  Comment

                  • clueless
                    Forum Member
                    • Jun 2006
                    • 421

                    #10
                    Thinking about it... its far easyer to convert the number to a string and then just return everything left of the decimal point;
                    Code:
                    function ConvertToInt(Anumb)
                    if String.Find(Anumb.."", ".", 1, false) > 1 then
                       Anumb = String.ToNumber(String.Left(Anumb.."" , String.Find(Anumb.."", ".", 1, false)-1));
                       end
                    return Anumb
                    end

                    Comment

                    • longedge
                      Indigo Rose Customer
                      • Aug 2003
                      • 2498

                      #11
                      Originally posted by clueless View Post
                      Thinking about it... its far easyer to convert the number to a string and then just return everything left of the decimal point;
                      Would you really want to return 1.99999999 as 1 ? If it comes down to ease with reasonable accuracy, I can't imagine anything easier than the single line of code in my earlier post - but perhaps I'm missing something

                      Comment

                      • Desolator
                        Forum Member
                        • Apr 2007
                        • 292

                        #12
                        Math.Round? :P

                        Anyway, AMS sends numbers as `long int', thus no floating-point numbers unless you make a plug-in.

                        Comment

                        • longedge
                          Indigo Rose Customer
                          • Aug 2003
                          • 2498

                          #13
                          Originally posted by Desolator View Post
                          Math.Round? :P

                          Anyway, AMS sends numbers as `long int', thus no floating-point numbers unless you make a plug-in.
                          I was working on principle that Math.Round reduces a real number to a specified number of decimal places whereas Math.Floor returns an integer. Yes OK you can specify zero decimal places and it's as short as it is long :lol

                          ..and what do I know anyway - I never listened to my Maths Teachers

                          Comment

                          • clueless
                            Forum Member
                            • Jun 2006
                            • 421

                            #14
                            I was adding a crop function to an animation editor im making. The user sellects the frame to be cropped which is passed to a cropping page which displays the picture with a rectangle in the center representing the area to be cropped. because the frame size is not fixed i needed to calculate the size of the cropping rectangle relative to the size of the frame. If you try to set the co-ordinates of a rectangle with anything other than whole numbers it just wont work.
                            Code:
                            FileInfo        = Image.GetFileInfo(LoadPic);
                            TheWidth        = FileInfo.Width;
                            TheHeight       = FileInfo.Height;
                            Ax              = GetInt(TheWidth  / 4);
                            Ay              = GetInt(TheHeight / 4);
                            Bx              = Ax        * 3
                            By              = Ay        * 3;
                            RectangleWidth  = Bx        - Ax;
                            RectangleHeight = By        - Ay;
                            
                            Image.SetSize("Image1", TheWidth, TheHeight);
                            Image.Load("Image1", DrawRectangle(LoadPic, Colors.White, 1, Ax, Ay,RectangleWidth, RectangleHeight));

                            Comment

                            Working...
                            X