decimal在 C# 中使用或之间有什么区别float?我知道float用于更精确的小数,并且decimal用于货币或价格等小数位数,但是当它用于小数位数时,为什么使用而不是decimal更好float?
我必须在我的数据库中表示数字,这些数字是食物中的化学物质,如脂肪,能量,镁等.这些值是12345.67格式的小数.
如果我decimal (5,2)在SQL Server中用作数据类型,它将映射到DecimalEntity Framework中的类型.如果我在SQL Server中使用float作为数据类型,它将映射到DoubleEntity Framework中.
我不确定SQL Server中最好的数据类型是什么,或者它真的不重要吗?
编辑 - 在我的情况下应该是decimal(7,2),正如一些评论中提到的那样!
谢谢.
我想解析一个字符串浮动.当字符串为null时,它将传递0(作为浮点值).
我正在做这样的解析:
aeVehicle.MSRP = float.Parse((drInvetory["MSRP"] ?? "0").ToString());
Run Code Online (Sandbox Code Playgroud)
哪个错误:
ERROR MESSAGE : Input string was not in a correct format.
ERROR SOURCE : at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseSingle(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Single.Parse(String s, NumberStyles style, NumberFormatInfo info)
at System.Single.Parse(String s)
Run Code Online (Sandbox Code Playgroud)
请建议处理这种情况的最佳方法.
我有两个值,一个来自用户输入,另一个来自DB.
var userinput = form["someInput"];
var valuefromDB = GetValue(someNumber);
public float? GetValue(int id){
return (float?) db.table.where(p=> p.id == id).select(p=> p.Value).SingleOrDefault();
}
Run Code Online (Sandbox Code Playgroud)
userinput的值为"1"作为字符串,而valuefromDB的值为0.001为float.
所以1/0.001 = 1000
但是我的c#代码给了我999.999939的结果;
var final = float.Parse(userinput) / valuefromDB
Run Code Online (Sandbox Code Playgroud)
当我有"2"作为用户输入值,结果是正确的,2000 ...
我有一个特定转换的有趣问题.当我尝试将字符串"0,3"或"0.3"根据值UICulture转换为Double值时,结果为0,29999999.我还没有找到解决方案,以便收到结果0,3.
转换后有什么方法可以获得相同的值吗?
VB或C#中"十进制"值的内部格式是什么?
我不知道这对我正在做的任何事都很重要,但这是其中一件很有趣的事情.比如,知道多少位和负数如何存储可能意味着当你看到一个负数出现在你预期的正面时,你可以立刻想到,"啊,有一个溢出",而不是被深深的黑暗奥秘困惑.
我试图比较两个Double变量Powershell.当变量超过2位数(不计算精度)时,相等测试意外失败.
我错过了一些明显的东西吗?
这是测试脚本输出:
PS C:\test> .\test.ps1
==========
TEST ONE
==========
Value of Double1: 336.1
Type of Double1: System.Double
-----
Value of Double2: 336.2
Type of Double2: System.Double
-----
Value of Double1+.1: 336.2
Type of Double1+.1: System.Double
-----
Does Double1 not equal Double2: True
Does Double1+.1 not equal Double2: True
==========
TEST TWO
==========
Value of Double3: 36.1
Type of Double3: System.Double
-----
Value of Double4: 36.2
Type of Double4: System.Double
-----
Value of Double3+.1: 36.2
Type …Run Code Online (Sandbox Code Playgroud) 我正在尝试将XML字符串解析为列表,结果计数始终为零.
string result = "";
string address = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
// Create the web request
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
// Read the whole contents and return as a string
result = reader.ReadToEnd();
}
XDocument doc = XDocument.Parse(result);
var ListCurr = doc.Descendants("Cube").Select(curr => new CurrencyType()
{ Name = curr.Element("currency").Value, Value = float.Parse(curr.Element("rate").Value) }).ToList();
Run Code Online (Sandbox Code Playgroud)
哪里出错了
我使用小数类型进行高精度计算(货币)。
但我今天遇到了这个简单的划分:
1 / (1 / 37)这应该再次得到 37
http://www.wolframalpha.com/input/?i=1%2F+%281%2F37%29
但 C# 给了我:
37.000000000000000000000000037M
我尝试了这两个:
1m/(1m/37m);
和
Decimal.Divide(1, Decimal.Divide(1, 37))
但两者产生相同的结果。这种行为如何解释?
我不理解术语二进制浮点和十进制浮点.我知道浮点数(32位)和双打(64位)是二进制浮点数,十进制数(128位)是十进制浮点数.我理解十进制是基数10,二进制是基数2.但是,我不明白为什么小数是基数10,因为最终一切都是基数2?为什么小数位数为10,而不是基数为2的浮点数和双精度数?
我今天看过很多网站,例如:.NET中十进制,浮点数和双倍数之间的差异?
c# ×8
.net ×2
decimal ×2
double ×2
types ×2
vb.net ×2
asp.net-mvc ×1
comparison ×1
equals ×1
powershell ×1
sql-server ×1
sqldatatypes ×1
variables ×1
webrequest ×1
xml ×1