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. Limiting to 2 decimal places

Limiting to 2 decimal places

Scheduled Pinned Locked Moved C#
tutorialquestion
4 Posts 4 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.
  • B Offline
    B Offline
    binglin
    wrote on last edited by
    #1

    int a = 1.76543 how to i print out in such it way it'll only display 1.76?

    S S E 3 Replies Last reply
    0
    • B binglin

      int a = 1.76543 how to i print out in such it way it'll only display 1.76?

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

      If you're sure you'll always get d.dddd, you can simply multiply it by 100 (or the number of digits you want after the decimal point, cast it to an int and divide back by 100. Something like

      double a = 1.76543;
      int x = ((int)(a * 100)))/ 100.00;

      Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      1 Reply Last reply
      0
      • B binglin

        int a = 1.76543 how to i print out in such it way it'll only display 1.76?

        S Offline
        S Offline
        Stefan Troschuetz
        wrote on last edited by
        #3

        a.ToString("0.00") should do the job. For some more information on formatting of numeric strings take a look at this page[^].


        www.troschuetz.de

        1 Reply Last reply
        0
        • B binglin

          int a = 1.76543 how to i print out in such it way it'll only display 1.76?

          E Offline
          E Offline
          esjq
          wrote on last edited by
          #4

          double a = 1.76543; Console.WriteLine("{0:F2}", a);

          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