I have the following problem I am new to working with C# and MVC I am doing maintenance on a program that is under development, that program searches for a product in real time and when saving it and displaying it in a pdf sheet the decimals are running to the left and add .00
Example
IN THE SEARCH ENGINE IT COMES: TELEPHONE 1550.55
WHEN SHOWING IT IN PDF SALE: 155055.00
Debugging I found the part that performs this process
I will only show the code to which it refers
In the Model you have a class called Item
public class Item
{
public Item()
{
precio = 0;
}
public decimal precio;
}
In the Controller you have a class called Orders
List<Item> items = JsonConvert.DeserializeObject<List<Item>>(collection["listaProductos"].Trim());
foreach (Item i in items)
{
int indexPrecio = i.nombre.IndexOf(" Precio: ");
if (indexPrecio != -1)
{
i.precio = Convert.ToDecimal(i.nombre.Split(new string[] {"Precio:" }, StringSplitOptions.None)[1].Trim());
i.nombre = i.nombre.Substring(0, indexPrecio);
}
}
In this line I am sure that it moves the decimals to the left and adds the 2 decimals (.00)
i.precio = Convert.ToDecimal(i.nombre.Split(new string[] {"Precio:" }, StringSplitOptions.None)[1].Trim());
I have tried to truncate or change to double but I have not been able to solve
I would be very grateful if you could help me or guide me to solve this error
What is needed to accept the correct value of the decimals is that the server understands what the correct format is, this can be achieved with the using System.Globalization library; . When the value of the string is converted to decimal, the overload with CultureInfo.InvariantCulture is applied.