存储在数据库中的十进制数以十进制小数形式返回.net Profile

时间:2016-08-11 11:21:06

标签: vb.net decimal asp.net-membership string-formatting

我有一个自定义.net配置文件,它在SQL表中存储一个十进制值

Column decCode = Numeric (3,1)

存储的值为0.8

我在web.config中的个人资料属性是

<add name="decCode" provider="SqlProfile" customProviderData="decCode,Decimal" type="System.Decimal" />

当我执行ProfileBase.GetPropertyValue("decCode")时,值似乎总是向上舍入。 (ProfileBase不是继承的,它是标准的aspnet配置文件类)

举个例子: (在DB中为0.8)

Dim dec As Decimal = CType(pc.GetPropertyValue("decCode"), Decimal) === 1D
Dim a1 As String = String.Format("{0}: {1}", "G", dec.ToString("G")) === 1
Dim a2 As String = String.Format("{0}: {1}", "C", dec.ToString("C")) === £1.00
Dim a3 As String = String.Format("{0}: {1}", "E04", dec.ToString("E04")) === 1.0000E+000
Dim a4 As String = String.Format("{0}: {1}", "F", dec.ToString("F")) === 1.00
Dim a5 As String = String.Format("{0}: {1}", "N", dec.ToString("N")) === 1.00
Dim a6 As String = String.Format("{0}: {1}", "P", dec.ToString("P")) === 100.00%
Dim a7 As String = dec.ToString() === 1
Dim a8 As String = dec.ToString("0.0") === 1.0

好像我使用linq获得decCode的值

return (_context.pUserProfiles.Where(p => p.UserId == GUID).FirstOrDefault().decCode).ToString()

我得到0.8

如何使用ProfileBase.GetPropertyValue返回正确的小数?

1 个答案:

答案 0 :(得分:1)

您必须在&#34; customProviderData&#34;中更改DECIMAL为DOUBLE和&#34; cType&#34;,如下:

 <add name="decCode" provider="SqlProfile" customProviderData="decCode,Double" type="System.Double" />

  Dim dec As Double = CType(pc.GetPropertyValue("decCode"), Double)