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.
– pnet
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...
– Dorathoto
Already tried @Convert.Todouble(Model.Display Value/ 100.0);
– Rafael Ferreira
@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
@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.
– Dorathoto
@Rafaelferreira puts as an answer that now was, congratulations
– Dorathoto
@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.– Maniero
@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
@Dorathoto
Model.ValorExibicao
is adecimal
orint
? I think I’m beginning to understand, you did the first calculation on model, and then the value goes wrong in view?– Maniero
Model.Display Value is an int...
– Dorathoto
Shouldn’t the answer be from @Rafaelferreira? I only read your comment.
– pnet
yes, but @Rafaelferreira did not post reply, only comment, has no valid mark.
– Dorathoto