Converting a double to an int

Can I convert a variable of type double to a variable of type int? The reason for this is I am creating a program that will draw graphs of parabolas, cubics, etc. and the user will be able to change the values for stretches, translations, etc.
So my code for the drawing of a parabola at the moment is somewhat primitive, I just want the basic parabola drawn in. I've done a program that does exactly this using Turing (of all things...) but Java is proving to be more picky.
int count = 0;
int X = 0;
double tempY = 0;
int Y = 0;
while(count <= 600){
     tempY = Math.pow(X - 300,2) + 200;
     Y = tempY;     //error here
     if(Y >= 0 && Y <= 400){
          g.fillOval(X, Y, 1, 1);
     count++;
     X++;
     }Don't worry about paintComponent() and all that, it's there I'm just posting the necessary parts you guys need to see.
So the error I get is 'possible loss of precision' at the line indicated, and I know this, it's pretty obvious, but this is what I want/need to do as the fillOval will only accept ints for it's coordinates. I also tried forgetting about having the double value of tempY and doing the math to an int but then I get 'possible loss of precision' in the math line. If anyone can give me ideas/fixes to change a double into an int it would be much appreciated as this is what I really want to do, or if you can come up with alternative ways of doing what I need to do that would also be appreciated.
EDIT
In case you are wondering why I am adding and subtracting those values it's because my panel I'm drawing on is (600,400) and of course (0,0) is the top left corner rather than the center where it would be on a Cartesian plane, so I think I've made all the necassary adjustments so (0,0) is now in essentially the center of the panel. It's basically scanning across from the left to right and finding X^2 from '-300' to '300' but only drawing it in if the value for Y will appear in the panel. I know it's pretty fucked up and probably some really messy code, lol. I'm somewhat inexperienced and probably being a bit too adventurous.
Edited by: vortex on Jan 15, 2008 11:07 AM
Edited by: vortex on Jan 15, 2008 11:15 AM

I haven't tried that, I will try that when I get to school. I think the issue could be that -300^2 may be too big to be an int? I was just thinking before I fell asleep about it last night and that could be it too...So naturally I'll just start from say, -100, or even -25 as that isn't exactly gonna fit on the graph so I'm just wasting processor power currently I suppose. Anyways I'm gonna try both of these out and get back to you guys.
Another issue I'm foreseeing is that one of the graphs is the root of x, so I'm going to be dealing with decimals, that's fine for double but when I turn that into an int (assuming I ever figure out how to, hoping your idea, possibly coupled with mine works) how can I round/drop the decimals?
Edited by: vortex on Jan 16, 2008 8:10 AM

Similar Messages

  • How do I convert a double to an int?

    I would like to use math.pow(), but math.pow() only take doubles as arguments and result and I need my result to be an int.
    Example:
    double a = 10;
    double b = 3;
    double c;
    c = math.pow(a,b);
    "Now, here I what to convert c to an int"

    Can't you say
    int d = Math.floor(c);
    ?

  • Converting a Double To A String

    Is it possible to convert a double to a string. I need to retrieve a double value from an array and display it as a label in an applet.
    How do I convert the double value to a string so that I can use it in the applet as a label?
    Thanks

    try this.
    double mydbl = 0;
    String mystr = null;
    mysstr=mydbl+"".trim();
    Paulthis one is nice paul, but it is also possible to do it that way:
    double d = 0;
    String dStr = Double.toString(d);see also API for classes Boolean, Character, Integer, Byte, Float, Short
    all primitives can easyli be turned int for in which you can print them out.

  • Converting a double to binary

    Hey all,
    I am having difficultly trying to devise a simple method for converting a double to its binary representation. The value that I am dealing with is ~10^32, so I cannot use int or long. Any advice/suggestions would be appreciated. Shanks.

    prometheuzz wrote:
    Boojum317 wrote:
    Value = 711051181013210910132971103265
    Calculated binary = 100011000100001111100110000111110000000110000000100101001101011
    Value of calculated binary = 5.0535875047965512E18I am not checking your numbers, but I suspect you are confused how a floating point is represented in a binary notation. This is different than an integer value.
    Read the link that was posted earlier in your thread.Indeed.
    Long.toBinaryString(Double.doubleToLongBits(1.0))
    gives
    11111111110000000000000000000000000000000000000000000000000000which is correct.

  • How can I convert a double to string

    Hello,
    how can I convert a double value to a string value?
    Werner

    an other way: ;)
    double yourDouble;
    int intTmp;
    yourDouble = yourDouble * 100;
    yourDouble = yourDouble + 0.5;
    intTmp = yourDouble.intValue();
    yourDouble = intTmp;
    yourDouble = yourDouble / 100;you can write it some shorter...
    double yourDouble;
    int intTmp;
    yourDouble = yourDouble * 100 + 0.5;
    yourDouble = yourDouble.intValue() / 100;*100 moves the comma about two steps to the left.
    + 0.5 enables rounding!
    if your value is x.5 or larger you get x+1.?
    if its smaller than x.5 you get x.?
    by parsing the double value to an int the ? is cut.
    /100 moves the comma two steps back
    you get a double value with 2 decimals which is rounded.
    just my coins :)
    happy progging,
    Thof

  • Vectors : Converting to double[ ] array

    Hello,
    I have a vector which I know consists of double values only. I am trying to create an array of double from this vector but am having some difficulty.
    Firstly if I try this :
    double[] high = (double[])datavector.high.toArray(new double[0]);JBuilder reports :
    'Cannot find method toArray(double[])'
    But if I try this :
    Double[] high = (Double[])datavector.high.toArray(new Double[0]);It works.
    So from this I assume 'Double' is not equal to 'double'
    The trouble is I require 'double[ ]' and NOT 'Double [ ]'.
    Casting Double as (double) does not work... so how do I get double[] from my original vector ?
    Many thanks
    Kerry

    double[] d = new double[v.size()];
              int i = 0;
              for(Iterator<Double> it = v.iterator(); it.hasNext();)
                   d[i++] = (double)it.next();
              just declare the double array to be the size of the vector, then loop thru and populate the array one at a time
    ~Tim

  • Help!!!!!  anyone know how to change a Double to an int??

    Help!!!!! anyone know how to change a Double to an int?? for use in an array[int].

    I don't know why would you want to depend on a double value to build your array but programmatically just use plain old cast: i = (int) double_value;
    Keep in mind though, the integer value will be truncated.

  • How can i convert a double to float

    double gallons;
    double startOdometer;
    double endOdometer;
    double mpg;
    double miles;
    endOdometer = Double.parseDouble (inputs);
    miles = endOdometer - startOdometer;                                                                 
    mpg = (miles/gallons) ;
    gallons = miles/mpg;
    mpg = Float.parseFloat(double);

    You would use a cast.
    double d = 1.4;
    float f = (float) d;But why do you want to convert a double into a float and then store it back into a double?????

  • Failed When converting nvarchar value SC1 to int (Cinf)

    Hai,
               In my screen conversion master after entering all the data's if i click addon button the error showing
    Conversion Failed When converting nvarchar value 'SC1' to int (Cinf)
    where SC1 is the object code of my form.
    Can anyone suggest what could be the problem?
    Regards

    Hi Anitha,
    Please check the variable @object_type in the SBO_SP_TransactionNotification stored procedure in your company database. In older versions of SBO, this had an int datatype. In 2007A or 2007B, this should be nvarchar(20) and you will get a conversion error when adding or updating UDO data if the variable is still an int datatype. Please see SAP Note 967470 for more details.
    Kind Regards,
    Owen

  • Creating polygons with an array of doubles instead of ints

    Hi,
    I want to create a polygon using:
    Polygon u = new Polygon(xPoints, yPoints, numTs);
    my problem is that my x & y points are stored in double arrays not ints. Is there a way to create a polygon using double arrays instead of ints?
    cheers,
    elmicko

    or cast them as int. imaginr posX, posY, width and height are doubles.
    Rectangle rect = new Rectangle( (int)posX, (int)posY, (int)width,(int)height );or, with an array of doubles:
    Rectangle rect = new Rectangle( (int)array[0], (int)array[1], (int)array[2],(int)array[3] );

  • Converting a byte array into int

    Here's my problem, I've read my data from a server into a byte array.
    the array is 12 elements in length representing three int variables.
    int flag;
    int query_a;
    int query_b;
    here's what i receive:
    0 0 0 0 34 0 0 -2 21 0 0 0
    how do i convert these into the int values i need.
    i know the first four are for flag = 0, but how does it convert?
    0000 = 0 for each byte
    00000000 00000000 00000000 00000000 = 0 for each bit?
    or is there a method to convert from a byte to int?

    Look at the ByteBuffer class (part of 1.4.1) - before that, you would have had to manually build your integers using left shift and & operator (not that big of a deal, really).
    Be sure you know the "Endian"-ness of the platform you are reading data from though, otherwise, your ints will not be what you expect!
    - K

  • How do I convert the unicode into an int?

    I have to take a large integer from user and put each digit into an array. To take the large number I use .nextLine() so that I would be stored as a string. Then I put each digit into an array of type int, and I use implicit type casting to convert each character to an int. The problem is that now the array contains the unicode for the number, how do I get the actual number?
    I know this is kind of silly not to use bigInteger but that's part of the assignment.
    better yet, the array mas as well be of bytes since no digits Unicode goes above 57
    Edited by: bean-planet on Oct 31, 2008 1:12 PM

    So your character is '1' and when you cast that to an int you get something like 49? And you want 1 instead? If that's the case then don't cast, use Character.getNumericValue('1') instead. But read the documentation for the method so you don't get surprised at what Character.getNumericValue("L") returns, for example.

  • How do i convert a double array (with spaces and tabs) to a string?

    Hi
    In our files, we have a mixture of spaces and tabs as a delimeter. How do I convert a double array into a string?
    Thank you.

    Not sure about the last part of your question.
    The Search and Replace pattern can be better than the simple Search and Replace string when you have to do complex searchs, such as detecting multiple spaces.
    Have a look at the attachment.
    CC
    Chilly Charly    (aka CC)
             E-List Master - Kudos glutton - Press the yellow button on the left...        
    Attachments:
    ReplaceSpaces.vi ‏27 KB

  • DrawLine using double instead of int

    I'm trying to use the java.awt to draw a line but I would like to use doubles instead of ints.
    the normal way would be to use drawline(int x, int y , int x2 , int y2);
    but i cant find a way to drawline( double x, double y, double x2, double y2)
    any idea's?
    Some more detail about this is I�m trying to create a package to draw a graph and my points are in double (and I would like them to stay that way) is there a way?
    Many Thanks, John

    if you want to keep it in doubles you could use Line2D.Double from the java.awt.geom package. Then instead of a call to drawLine(int, int, int, int) call draw(line2D) passing in the line2D. As it implements the shape interface it will know what to do. Note to do this call you'll have to cast the Graphics object to a Graphics2D.
    Hope this helps
    Mike

  • Double into an int?

    Hi,
    Bit of a noob question, can anybody tell me how to parse a double into an int?
    many thanks

    Of course, if you want behavior other than truncation, look at the Math class.
    double myDouble = 3.97;
    int myInt = (int)Math.round( myDouble ); // myInt == 4
    int myInt = (int)Math.floor( myDouble ); // myInt == 3
    int myInt = (int)Math.ceil( myDouble ); // myInt == 4RD-R
    � {�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Maybe you are looking for

  • Cant sync my ipod touch

    when i try to sync ituns say "sync ipod" but never sync, some times when happen that i find in help (itunes) and press updates, and when itunes find updates sync the ipod, but cant change my music or apps, only can make the backup. i update the last

  • Invoice and Inventory App

    I run a small clinic with one acupuncturist and would like to track her daily sales on her iPad. Ideally, she'd write up an invoice (no need for printing) with the treatment cost and the supplements purchased for each patient. Then at the end of the

  • Table control in custom infotypes

    Hi, I have a requirement to add table control in a custom defined infotype. The fields include two date fields, one icon field, number and name fields. I also need to populate my icon field with three different icons according to the modification in

  • How to execute bapi programm

    hai       i have a BAPI programme to delete the orders.because i need to delete more than 600 orders, can any one tell me how to execute that programme.but we can use lsmw and bdc also. my abaper has given me that programme BAPI_SALESORDER_CHANGE TO

  • Does anyone know the step by step sequence for unlocking bell mobility phones?

    I have the Blackberry tour 9630 if any has the sequence on how to do it? Thanks