Display decimals (Currency) is rounding

Asked

Viewed 2,098 times

4

I have an application where the price should go in format int decimal-free.

int Valor = (int)(produto.Valor * 100); //o produto.Valor é um decimal

The problem is when I want to display this value in the View

In the case of products like 0.10 it displays 0

int Valor = (int)(produto.Valor * 100);

o Value is 10, equivalent to 0.10 (perfect so far)

I tried to:

@{
decimal Valor = Model.ValorExibicao / 100; //ValorExibicao é um int
}
@Valor.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("pt-br"))

Displays R$ 0,00

I tried to:

@Convert.ToDouble(Model.ValorExibicao/ 100);

Displays 0 Only @Model.ValorExibicao shows the correct 10 (equivalent to 0.10)

Could create in the viewModel the correct value without formatting, but I think it would be a trick to circumvent an error of mine.

  • Isn’t it because value is declared as an int? When you "casteia" the decimal for an int, it will lose the decimal part. If it was 0.10, it will be 0. Do not use int for these operations, work straight with decimal.

  • but the system (Cielo) asks for the price in int. Why to convert I divide by 100 and in theory would return to decimal in the case double, float, decimal...

  • 1

    Already tried @Convert.Todouble(Model.Display Value/ 100.0);

  • @Dorathoto, price in int I believe should not exist. Values are cents(divided by 100) and will almost always have the value with decimals. It makes no sense to have an integer number to represent a value divided by 100 (decimal places).

  • @pnet, always use price in decimal, however the 3rd system is asking in int format. The question was not whether I should store in int or decimal.

  • @Rafaelferreira puts as an answer that now was, congratulations

  • 1

    @Dorathoto conversion to double is going to be a problem, so you have to understand how things work and not rely on the basic test you do, has value that works, has value that doesn’t work, will leave your system like this? Actually I didn’t understand your problem, much less why a division solves the problem. If you want a penny value to be integer (scaling up) the correct is multiplication.

  • @bigown is actually a field that I will exbir for the customer, when sending the value to the third party system I need to send it whole, ex: R$ 25,36 = 2536 for that I took my decimal value and multiplied by 100, so far perfect, the problem is now that I want to display to the customer the value, without having to recover again from the database, or having to create a new field in viewmodel just for this, I thought to divide by 100 and return to the original value, but could not.

  • @Dorathoto Model.ValorExibicao is a decimal or int? I think I’m beginning to understand, you did the first calculation on model, and then the value goes wrong in view?

  • Model.Display Value is an int...

  • Shouldn’t the answer be from @Rafaelferreira? I only read your comment.

  • yes, but @Rafaelferreira did not post reply, only comment, has no valid mark.

Show 7 more comments

2 answers

3

There are other ways to get the same result, like everything in the programming. You can make a Explicit cast for decimal. Use the decimal. Parse() to convert the values (much used when the value is in string). You can also use the Convert.Todecimal(). These three options will return you the entire value with decimal places.

Below I will leave an example of how to use each of the above mentioned forms:

public static void Main()
{
    int valor = 10;
    string valorString = "10";

    decimal valorNormal = valor/100;

    decimal valorCast = (decimal)valor/100;

    decimal valorParse = decimal.Parse(valorString)/100;

    decimal valorConvert = Convert.ToDecimal(valor)/100;

    Console.WriteLine("Valor Normal: " + valorNormal);
    Console.WriteLine("Cast explítico: " + valorCast);
    Console.WriteLine("Parse: " + valorParse);
    Console.WriteLine("Converto to: " + valorConvert);

}

Example working on dotNetFiddle.

2


The solution is very simple:

Model.ValorExibicao / 100M

This ensures that the division will be done in decimal, since the suffix M indicates a decimal literal. Then you want to apply a ToString(), one string.Format(), or a string interpolated, or another way to format the display, decimals, is at your discretion, the calculation will be right in this way.

Never use double for monetary values or others which cannot have rounding problems.

Browser other questions tagged

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