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. Truncate decimal in float

Truncate decimal in float

Scheduled Pinned Locked Moved C#
question
5 Posts 3 Posters 0 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.
  • E Offline
    E Offline
    edel_ong
    wrote on last edited by
    #1

    How do I truncate a float such that if i have 1.20001, i would want to have 1.2 only for display. How could I achieve this? Thank you very much!

    S 1 Reply Last reply
    0
    • E edel_ong

      How do I truncate a float such that if i have 1.20001, i would want to have 1.2 only for display. How could I achieve this? Thank you very much!

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      If you just want the truncated value as a string, you can use the ToString method on the float instance, like

      float f = 1.2001f;
      string truncatedFloat = f.ToString("0.0");

      If you want that value as a float, you could multiply it by 10x, where x is the number of digits of precision required, cast it to an int and divide by 10x again i.e 1.20001 * 10 = 12.0001 = 12 / 10 = 1.2

      Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

      E G 2 Replies Last reply
      0
      • S S Senthil Kumar

        If you just want the truncated value as a string, you can use the ToString method on the float instance, like

        float f = 1.2001f;
        string truncatedFloat = f.ToString("0.0");

        If you want that value as a float, you could multiply it by 10x, where x is the number of digits of precision required, cast it to an int and divide by 10x again i.e 1.20001 * 10 = 12.0001 = 12 / 10 = 1.2

        Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

        E Offline
        E Offline
        edel_ong
        wrote on last edited by
        #3

        works perfectly well. Thanks!

        1 Reply Last reply
        0
        • S S Senthil Kumar

          If you just want the truncated value as a string, you can use the ToString method on the float instance, like

          float f = 1.2001f;
          string truncatedFloat = f.ToString("0.0");

          If you want that value as a float, you could multiply it by 10x, where x is the number of digits of precision required, cast it to an int and divide by 10x again i.e 1.20001 * 10 = 12.0001 = 12 / 10 = 1.2

          Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          S. Senthil Kumar wrote:

          If you want that value as a float, you could multiply it by 10x, where x is the number of digits of precision required, cast it to an int and divide by 10x again i.e 1.20001 * 10 = 12.0001 = 12 / 10 = 1.2

          Note that this is not guaranteed to produce the exact value that you expect. A float only has a precision of seven digits, so the result may just as well be something like 1.19999 or 1.20001. And why not use Math.Round(f, 1)?

          --- b { font-weight: normal; }

          S 1 Reply Last reply
          0
          • G Guffa

            S. Senthil Kumar wrote:

            If you want that value as a float, you could multiply it by 10x, where x is the number of digits of precision required, cast it to an int and divide by 10x again i.e 1.20001 * 10 = 12.0001 = 12 / 10 = 1.2

            Note that this is not guaranteed to produce the exact value that you expect. A float only has a precision of seven digits, so the result may just as well be something like 1.19999 or 1.20001. And why not use Math.Round(f, 1)?

            --- b { font-weight: normal; }

            S Offline
            S Offline
            S Senthil Kumar
            wrote on last edited by
            #5

            Guffa wrote:

            Note that this is not guaranteed to produce the exact value that you expect.

            Agreed, I missed the fact that the floating point representation could screw up things. But if he already uses tolerance values when checking for equality among floats, this shouldn't matter, right?

            Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

            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