Keep decimal type data in n decimal places without rounding (C #)

Source: Internet
Author: User

A description of the problem

In development, you need to keep the decimal type of data two decimal places after the decimal point and do not need to be rounded, that is, directly intercept the two decimal places after the decimal point. Example:1.245M--1.24, not 1.25

You can use the decimal.round () method to preserve several decimal places after the decimal of the decimal type data, but the method will be rounded instead of directly intercepted. Therefore, this method is not advisable, search on the internet and did not find a suitable method, they have implemented a truncated decimal type data decimal point after a number of methods.

Two detailed implementation code

Using system;namespace cutdecimal{public    static class Decimalhelper    {public        static decimal Cutdecimalwithn (decimal d, int n)        {            string strdecimal = D.tostring ();            int index = Strdecimal.indexof (".");            if (index = =-1 | | Strdecimal.length < INDEX + n + 1)            {                Strdecimal = string. Format ("{0:f" + n + "}", D);            }            else            {                int length = index;                if (n! = 0)                {                    length = index + n + 1;                }                Strdecimal = strdecimal.substring (0, length);            }            return Decimal.Parse (Strdecimal);}}    

Three test codes and results

static void Main (string[] args) {    decimal d = 1.23456789M;    for (int i = 0; I <=; i++)    {        Console.WriteLine ("{0}", Decimalhelper.cutdecimalwithn (d, i));    }    for (int i = 0; I <=; i++)    {        Console.WriteLine ("{0}", Decimal.round (d, i));}    }
in the test code, not only the test code of the implementation method in this paper is given, but also the test code of the Decimal.round () method, and the result is as shown in the final operation.

Figure 1 running results

Can you see the difference, folks?

Keep decimal type data in n decimal places without rounding (C #)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.