关于来自数据库的C#值 [英] About C# value from database

查看:75
本文介绍了关于来自数据库的C#值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在Sql Server DB中的值为1.000。但是当我将它读到Datatable时,它将修剪为2位数为1.00。我怎样才能将Datatable的原值读作1.000而不是1.00



请帮忙。



By我只使用.net 2.0框架工作的方式。



谢谢



我是什么尝试过:



我必须尝试使用​​todecimal,Math.Round等进行转换。但是不起作用。

Hi All,

I am having a value in Sql Server DB as 1.000. But when i am reading it to Datatable it is trimming to 2 digits as 1.00. How can i read the original value to Datatable as 1.000 instead of 1.00

Please help.

By the way i am using .net 2.0 frame work only.

Thanks

What I have tried:

I have to tried to convert using todecimal, Math.Round etc. But does not work.

推荐答案

这个问题毫无意义。 1.000和1.00是完全相同的数字。什么都没有修剪。看起来您需要理解数字的概念,您将其与文本表示混淆。如果是这样的话,很难帮助它,这是基础教育的基础问题。



现在,为了控制数字的字符串表示,应该使用格式化。例如,请参阅:

Double.ToString方法(系统)

标准数字格式字符串

自定义数字格式字符串



-SA
The question makes no sense at all. 1.000 and 1.00 is the same exact number. Nothing is "trimmed". It looks like you need to understand the concept of "number", which you confuse with its textual representation. If so, it's pretty hard to help with that, it's a matter of fundamental, elementary education.

Now, to control string representation of numbers, formatting should be used. For example, please see:
Double.ToString Method (System),
Standard Numeric Format Strings,
Custom Numeric Format Strings.

—SA


我会说使用十进制作为sql上的数据类型,例如。

I'd say use decimal as your data type on sql with a scale of 3 eg.
DECLARE @number DECIMAL(16,3) = 1
SELECT @number



您的结果将返回1.000



声明相同的数据类型variabl e on c#eg。


Your result will return 1.000

Declare same data type variable on c# eg.

decimal number = 1.000m;//value from db



该值将返回1,因为1和1.000是相同的,但因为你仍然想要它有3个小数位,你必须进行字符串格式化,例如。


the value will be returned as 1 because 1 and 1.000 is the same thing, but because you still want it with 3 decimal places you have to do string formating eg.

textBox1.Text = number.ToString("N3");



这将返回1.000


This will return 1.000


这篇关于关于来自数据库的C#值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆