How to truncate decimal in X decimal places?

Asked

Viewed 3,813 times

0

I am using the following code, but when sending ex:'10.100' to 2 houses it returns '10.1', but it should be '10.10'

    public decimal TruncarDecimal(decimal value, int decimalPlaces)
    {
        decimal integralValue = Math.Truncate(value);

        decimal fraction = value - integralValue;

        decimal factor = (decimal)Math.Pow(10, decimalPlaces);

        decimal truncatedFraction = Math.Truncate(fraction * factor) / factor;

        decimal result = integralValue + truncatedFraction;

        return result;
    }
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

3

Supports yes, see the documentation. There you have without the parameter of decimals, with this information and even as the rounding criterion should be.

Math.Round(valorDecimal, 2);

If what you want is not rounding then do the Truncate() climbing:

Math.Truncate(100 * valorDecimal) / 100;

I put in the Github for future reference.

But this is basically what’s in the question.

  • But I can’t round, the round changes the value

  • I answered what was in the question, now you say you can not do what you asked to do. I think the question is not clear then.

  • I quoted the Round example that I can choose decimal places, but it would need to be the truncate, so I could choose the houses, When I use Math.Truncate(decimal), the parameter is only the value.

  • The Truncate() also changes the value, you need to decide if you want to change the value or not. On the other hand if it is the Truncate() who wants to use, use it, do not know the purpose of the question in this case.

  • I will try to summarize for you understand, I do multiplications and the results give ex: 1,64857, I need to return the value with X decimal places depending on each case, I will not say more about truncate or Math, I need to do this.

  • If you don’t make it clear what you want, you can’t answer it the way you want. I’ve already coclqoeui two possibilities, if neither of the two solves, I think and you can’t tell what’s wrong with these solutions. the problem is that you do not know what you want to do. There is no way to give an answer, when you do not know what the question is. What you asked was answered, if you wanted to ask something else, you would need to make it clear.

Show 2 more comments

0

Try using the

Math.Truncate

for example:

using System;

class Program
{
    static void Main()
    {
        decimal a = 1.223M;
        double b = 2.913;

        a = Math.Truncate(a);
        b = Math.Truncate(b);

        Console.WriteLine(a);
        Console.WriteLine(b);
    }
}

Exits:

1
2

Now if you need to display the value with two decimal places use the code:

decimalVar.ToString("#.##");

More information on Math.Truncate see this link: https://social.msdn.microsoft.com/Forums/pt-BR/fbf78553-93ab-4a96-9e1b-39160df5d216/truncar-variavel-para-apenas-2-decimais?forum=vscsharppt

  • If you can add a link with the reference of some documentation that explains what the Math.Truncate.

  • Added the link https://social.msdn.microsoft.com/Forums/pt-BR/fbf78553-93ab-4a96-9e1b-39160df5d216/truncar-variavel-para-apenas-2-decimais?forum=vscsharppt

  • I need to display exactly with specific houses, the truncate does not let me choose the number of houses. And I’m trying to do it in a generic way in a method that passes value parameter and number of decimals.

  • @Brunorodrigues do you need to round up or down? If yes, where we worked we used the sum of + 0.4 in the value, so it increased upwards, if you use -0.4 it decreases downwards... there truncates naturally, I do not know if it is your case, but try testing

Browser other questions tagged

You are not signed in. Login or sign up in order to post.