Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do I cast a double to an int?

How do I cast a double to an int?

Scheduled Pinned Locked Moved C#
databasehelpquestionxml
21 Posts 7 Posters 11 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Justiin1265

    Hi, I have a program for work that uses a formula to calculate the refurb on a unit(parts replaced on cableboxes that were damaged) divided by total units(cableboxes that went through refurb, but did not have any parts replaced). I looked up casting online, and the format for it is intvaluetoconvert = Convert.toint32; I'm doing that , but still get the error Cannot implicitly convert type 'double to int. An explicit conversion exists(are you missing a cast?) What am I doing wrong? Can someone please help? Thank you. Justin Here's some of my code to look at? private int GetRefurbRate() { string sql = ""; double Refurb_Rate; int totalRefurb = 0; int totalUnits = 0; string error_msg = ""; sql = "SELECT COUNT(rp.repair_ord) " + "FROM " + schema + ".repair_part rp " + "WHERE rp.repair_ord = '" + repair_ord + "' "; while (true) { if (!myDb.RunSql(sql, true)) { error_msg = "DBError for getting Refurb Rate"; break; } if (myDb.dbRdr.HasRows) { if (myDb.dbRdr.Read()) { try //Try and Catch are here b/c I originally had everything ints, and and they just caught the 0 exception. { Refurb_Rate = Convert.ToInt32( totalRefurb / totalUnits * 100); //This is where I try to perform the cast. } catch (Exception e) { Console.WriteLine(e); } } //int Refurb_Rate = Convert.ToInt32(Refurb_Rate); } break; } myDb.dbRdr.Close(); if (error_msg != String.Empty) { MessageBox.Show(error_msg, "Get Refurb Rate", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } return Refurb_Rate; }

    C Offline
    C Offline
    charles henington
    wrote on last edited by
    #21

    Give this a try this should fix your problem;

    return (int)Refurb_Rate;

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups