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. Truncate decimal place

Truncate decimal place

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

    I need to do some math that involves truncating the number after the tenths place. So, I need to take some calculation and remove all digits after the first decimal place without rounding. For example 3765/3700 * 100=101.75675675675675675675675675676 But, I need to produce from that calculation 101.7. Not 101.8 (the rounded form). Is there a built in method that performs this sort of calculation, or some other way that you might suggest? Thanks.

    G M N A M 5 Replies Last reply
    0
    • P polishprogrammer

      I need to do some math that involves truncating the number after the tenths place. So, I need to take some calculation and remove all digits after the first decimal place without rounding. For example 3765/3700 * 100=101.75675675675675675675675675676 But, I need to produce from that calculation 101.7. Not 101.8 (the rounded form). Is there a built in method that performs this sort of calculation, or some other way that you might suggest? Thanks.

      G Offline
      G Offline
      gauthee
      wrote on last edited by
      #2

      try math.ceiling or math.floor methods

      Gautham

      1 Reply Last reply
      0
      • P polishprogrammer

        I need to do some math that involves truncating the number after the tenths place. So, I need to take some calculation and remove all digits after the first decimal place without rounding. For example 3765/3700 * 100=101.75675675675675675675675675676 But, I need to produce from that calculation 101.7. Not 101.8 (the rounded form). Is there a built in method that performs this sort of calculation, or some other way that you might suggest? Thanks.

        M Offline
        M Offline
        Martin 0
        wrote on last edited by
        #3

        Hello, If you want to show it as a string, you could use the String.Format method.

        int numberofdecpoints= 10;
        double d = 3765/3700 * 100;
        string roundednumber = String.Format("{0:F"+ numberofdecpoints.ToString() +"}",d);

        All the best, Martin

        1 Reply Last reply
        0
        • P polishprogrammer

          I need to do some math that involves truncating the number after the tenths place. So, I need to take some calculation and remove all digits after the first decimal place without rounding. For example 3765/3700 * 100=101.75675675675675675675675675676 But, I need to produce from that calculation 101.7. Not 101.8 (the rounded form). Is there a built in method that performs this sort of calculation, or some other way that you might suggest? Thanks.

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          May not be the prettiest double d = 101.75675675675675675675675675676; string s = string.Format("{0:f2}", d); d = Convert.ToDouble(s.Remove(s.Length-1)); Using the specifier {0:f1} still rounds the number to 101.8, so use 0:f2 and truncate it.


          only two letters away from being an asset

          1 Reply Last reply
          0
          • P polishprogrammer

            I need to do some math that involves truncating the number after the tenths place. So, I need to take some calculation and remove all digits after the first decimal place without rounding. For example 3765/3700 * 100=101.75675675675675675675675675676 But, I need to produce from that calculation 101.7. Not 101.8 (the rounded form). Is there a built in method that performs this sort of calculation, or some other way that you might suggest? Thanks.

            A Offline
            A Offline
            althamda
            wrote on last edited by
            #5

            Can't you just do this? double d = 128.158676; Convert.ToDouble(Convert.ToInt32(d * 10)) / 10;

            1 Reply Last reply
            0
            • P polishprogrammer

              I need to do some math that involves truncating the number after the tenths place. So, I need to take some calculation and remove all digits after the first decimal place without rounding. For example 3765/3700 * 100=101.75675675675675675675675675676 But, I need to produce from that calculation 101.7. Not 101.8 (the rounded form). Is there a built in method that performs this sort of calculation, or some other way that you might suggest? Thanks.

              M Offline
              M Offline
              mabo42
              wrote on last edited by
              #6

              double d = 101.75675675675675675675675675676;
              d = Math.Floor(10 * d) / 10;

              Now d has a value of 101.7 Hope this helps

              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