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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. double -> float : casting or Convert.ToSingle ?

double -> float : casting or Convert.ToSingle ?

Scheduled Pinned Locked Moved C#
csharpquestion
3 Posts 3 Posters 7 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.
  • C Offline
    C Offline
    crushinghellhammer
    wrote on last edited by
    #1

    Hi, I want to convert variables of type double to type float in my C# app. I've read that casting from a double to float results in the double being rounded to the nearest float value. That is exactly what I need (as opposed to truncation). However, I've also seen that the Convert.ToSingle(double) method uses rounding as well to convert the double to a float. Is there any difference between these two techniques - casting and ToSingle? Are there scenarios in which one is preferable over the other? What about overhead? Thanks!

    M N 2 Replies Last reply
    0
    • C crushinghellhammer

      Hi, I want to convert variables of type double to type float in my C# app. I've read that casting from a double to float results in the double being rounded to the nearest float value. That is exactly what I need (as opposed to truncation). However, I've also seen that the Convert.ToSingle(double) method uses rounding as well to convert the double to a float. Is there any difference between these two techniques - casting and ToSingle? Are there scenarios in which one is preferable over the other? What about overhead? Thanks!

      M Offline
      M Offline
      mikker_123
      wrote on last edited by
      #2

      I don't know technical point of this but from my personal expirience I ecourage everyone to use Convert.To... over casting because several times it happened to me that (casting) produced error and on contratry Convert.To... never made any trouble.

      1 Reply Last reply
      0
      • C crushinghellhammer

        Hi, I want to convert variables of type double to type float in my C# app. I've read that casting from a double to float results in the double being rounded to the nearest float value. That is exactly what I need (as opposed to truncation). However, I've also seen that the Convert.ToSingle(double) method uses rounding as well to convert the double to a float. Is there any difference between these two techniques - casting and ToSingle? Are there scenarios in which one is preferable over the other? What about overhead? Thanks!

        N Offline
        N Offline
        Nick Parker
        wrote on last edited by
        #3

        crushinghellhammer wrote: Is there any difference between these two techniques - casting and ToSingle? The basic difference is that a cast can thrown an exception which means anytime you cast something you should also wrap it in a try/catch block to handle any exception that is thrown. This in turn emits additional IL instructions, not much but it's there. - Nick Parker
        My Blog | My Articles

        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