asp.net-core保存浮点数为整数

huangapple go评论75阅读模式
英文:

asp.net-core save float as int

问题

I'm working on this application in asp.net core 6.0 where I'm trying to save a float value (in this case 0.4) and it's being saved as an int with a value of 4. I don't understand why the class has a value of 4, but when checking the model state, the value of the "water" variable is 0.4 (the correct one).

我正在开发一个在asp.net core 6.0中的应用程序,我尝试保存一个浮点值(在这种情况下为0.4),但它被保存为一个整数,值为4。我不明白为什么该类的值为4,但在检查模型状态时,“water”变量的值为0.4(正确的值)。

Here we have the class:

这是该类:

public class RPGH01_01
{
   public int ID { get; set; }
   [Display(Name="Toma agua*")]
   public float Water { get; set; }
   [Display(Name = "Fecha")]
   public DateTime Date { get; set; }
   [Display(Name = "Valor cloro**")]
   public float Chlorine { get; set; }
   [Display(Name = "Color")]
   public bool Color { get; set; }
   [Display(Name = "Olor")]
   public bool Smell { get; set; }
   [Display(Name = "Turbidez")]
   public bool Turbidity { get; set; }
   [Display(Name = "Sabor")]
   public bool Flavor { get; set; }
   [Display(Name = "Observaciones")]
   public string Observations { get; set; }
   public int UserID { get; set; }
}

Values of the class:

类的值:

Values

and value of the input and the model state validation:

输入的值和模型状态验证:

public async Task<IActionResult> Create([Bind("ID,Water,Date,Chlorine,Color,Smell,Turbidity,Flavor,Observations,UserID")] RPGH01_01 rPGH01_01)
{
   //This is to know what user create the report
   var userAccountId = HttpContext.Session.GetString("user");
   rPGH01_01.UserID = Convert.ToInt32(userAccountId);

   if (ModelState.IsValid)
   {
     _context.Add(rPGH01_01);
     await _context.SaveChangesAsync();
     return RedirectToAction(nameof(Index));
   }
   return View(rPGH01_01);
}

modelstate validation

Thanks for your time.

谢谢您的时间。

英文:

I'm working on this application in asp.net core 6.0 where I'm trying to save a float value (in this case 0.4) and it's being saved as an int with a value of 4. I don't understand why the class has a value of 4, but when checking the model state, the value of the "water" variable is 0.4 (the correct one).
Here we have the class:Class

public class RPGH01_01
{
   public int ID { get; set; }
   [Display(Name=&quot;Toma agua*&quot;)]
   public float Water { get; set; }
   [Display(Name = &quot;Fecha&quot;)]
   public DateTime Date { get; set; }
   [Display(Name = &quot;Valor cloro**&quot;)]
   public float Chlorine { get; set; }
   [Display(Name = &quot;Color&quot;)]
   public bool Color { get; set; }
   [Display(Name = &quot;Olor&quot;)]
   public bool Smell { get; set; }
   [Display(Name = &quot;Turbidez&quot;)]
   public bool Turbidity { get; set; }
   [Display(Name = &quot;Sabor&quot;)]
   public bool Flavor { get; set; }
   [Display(Name = &quot;Observaciones&quot;)]
   public string Observations { get; set; }
   public int UserID { get; set; }

}

Values of the class:Values
and value of the input and the model state validation:

public async Task&lt;IActionResult&gt; Create([Bind(&quot;ID,Water,Date,Chlorine,Color,Smell,Turbidity,Flavor,Observations,UserID&quot;)] RPGH01_01 rPGH01_01)
{
   //This is to know what user create the report
   var userAccountId = HttpContext.Session.GetString(&quot;user&quot;);
   rPGH01_01.UserID = Convert.ToInt32(userAccountId);

   if (ModelState.IsValid)
   {
     _context.Add(rPGH01_01);
     await _context.SaveChangesAsync();
     return RedirectToAction(nameof(Index));
   }
   return View(rPGH01_01);
}

modelstate validation
Thanks for your time

I have tried inserting other values (such as 0.4), but it rejects them because they are text. I have changed the variables to see if it worked by using double instead of float.

答案1

得分: 0

这很可能是由于你配置的区域设置所致。

在美国,小数点用作十进制分隔符,而逗号用作分隔符。但这些分隔符通常会被忽略。

float.Parse("4,500", CultureInfo.GetCultureInfo("en-US")); // 4500
float.Parse("4,5", CultureInfo.GetCultureInfo("en-US"));   // 45
float.Parse("0,4", CultureInfo.GetCultureInfo("en-US"));   // 4

在智利,逗号用作小数分隔符,而句点用作分隔符。

float.Parse("0.4", CultureInfo.GetCultureInfo("es-CL"))    // 4
float.Parse("0,4", CultureInfo.GetCultureInfo("es-CL"))    // 0.4

这可能不是一个错误:如果你的目标受众来自以这种方式表示数字的文化,用户可能会期望输入"0,4"来表示0.4。

如果你有来自各种文化的用户,你需要找到一种确定用户期望的区域设置并在请求中设置它的方式。

英文:

This is most likely due to your configured culture.

In the United States, a period is used as a decimal marker while commas are used as separators. But those separators are largely ignored

float.Parse(&quot;4,500&quot;, CultureInfo.GetCultureInfo(&quot;en-US&quot;)); // 4500
float.Parse(&quot;4,5&quot;, CultureInfo.GetCultureInfo(&quot;en-US&quot;));   // 45
float.Parse(&quot;0,4&quot;, CultureInfo.GetCultureInfo(&quot;en-US&quot;));   // 4

In Chile, the comma is used as a decimal marker and the period is used as a separator.

float.Parse(&quot;0.4&quot;, CultureInfo.GetCultureInfo(&quot;es-CL&quot;))    // 4
float.Parse(&quot;0,4&quot;, CultureInfo.GetCultureInfo(&quot;es-CL&quot;))    // 0.4

This may not be a bug: if your intended audience comes from a culture that represents numbers this way, users might actually expect to enter &quot;0,4&quot; instead to represent 0.4.

If you have users from a variety of cultures, you'll need to find a way to determine the user's expected culture and set it on the request.

huangapple
  • 本文由 发表于 2023年4月11日 04:51:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980608.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定