truncating but not rounding - string output of a decimal type value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bodycode
    New Member
    • May 2010
    • 5

    truncating but not rounding - string output of a decimal type value

    I am writing a dietary application usng Visual C# 2010. I need to output a decimal value that can be truncated to any number of decimal places I want, without rounding at all, on any level.

    I want to output the answer of a cross multiply and divide setup of 3 numeric up down control values, and output to a label without rounding at all. Each numeric up down control, is set to have 3 decimal places. Unfortunately, using math.truncate(v alue * 1000) / 1000 seems to output with accuracy errors (only found using more nuds). However, I do NOT want to use math.truncate any more. I want to use "pure" string output functions to accomplish that goal.

    I'll also be using other nuds (numeric up down contorls) for other functions, and need perfect fractional equivalencies if testing the string output for accuracy.
  • Monomachus
    Recognized Expert New Member
    • Apr 2008
    • 127

    #2
    Originally posted by bodycode
    I am writing a dietary application usng Visual C# 2010. I need to output a decimal value that can be truncated to any number of decimal places I want, without rounding at all, on any level.

    I want to output the answer of a cross multiply and divide setup of 3 numeric up down control values, and output to a label without rounding at all. Each numeric up down control, is set to have 3 decimal places. Unfortunately, using math.truncate(v alue * 1000) / 1000 seems to output with accuracy errors (only found using more nuds). However, I do NOT want to use math.truncate any more. I want to use "pure" string output functions to accomplish that goal.

    Please, can someone help? I'll also be using other nuds (numeric up down contorls) for other functions, and need perfect fractional equivalencies if testing the string output for accuracy. Can someone help?

    Thanks. computerdoctor@ nyc.rr.com
    Dude, try to gooogle for it. This time I'll answer, next time u'll get your LMGTFY link.

    1. Use ToString methods with formating:
    Example: number.ToString ("F4") - will display a number with four digits after comma. Also number.ToString ("#0.000") will help.

    Useful links

    Double.ToString Method (String) and Standard Numeric Format Strings

    Comment

    • bodycode
      New Member
      • May 2010
      • 5

      #3
      I think your solution is flawed, if my memory serves correctly. It creates rounding.

      LMGTFY is an inappropriate response since I've searched diligantly for more than a month.
      Last edited by bodycode; May 3 '10, 12:47 AM. Reason: redaction

      Comment

      • bodycode
        New Member
        • May 2010
        • 5

        #4
        Tested and found to be flawed. Output gets rounded.

        number.ToString ("#0.000") results in an answer of 76.410. However, the true value is 76.4096351. The second and third digits of the decimal are getting rounded! Why is that? I want to avoid that. I want pure truncation, no rounding. This error is entirely unacceptable.

        I set up the program correctly, but, this error creeps in in the output despite all attempts at research and trying different code snippets.

        Comment

        • bodycode
          New Member
          • May 2010
          • 5

          #5
          Originally posted by Monomachus
          Dude, try to gooogle for it. This time I'll answer, next time u'll get your LMGTFY link.

          1. Use ToString methods with formating:
          Example: number.ToString ("F4") - will display a number with four digits after comma. Also number.ToString ("#0.000") will help.

          Useful links

          Double.ToString Method (String) and Standard Numeric Format Strings
          Slight rounding still occurs but it's close enough with the F4 string number/string formatting. I acquiesce to your programming opulance :)

          Comment

          • bodycode
            New Member
            • May 2010
            • 5

            #6
            No, slight errors not acceptable. Need precise truncation, no rounding.

            I still need to merely "lop off" the resultant decimal numbers after the whole number answer, to the number of places I desire. Any error is not acceptable. The reason why I need no errors, is because the results will be used in future calculations in code, as well as some special graphing functions I'll need to incorporate into future graph results. Rounding errors, even slightly off the original precision will produce vastly innacurate results.

            I'm shocked that my need is a unique one!

            Anyone got a solution, please, by all means, shoot!

            I've found that "F3" format string is good, but still, sometimes, there's an error creeps in (I think).

            I think I can use some complex code that extracts numbers from strings, variables, or array positions that will result in a selectable number of digits.

            Marshall

            Comment

            Working...