ToString转换问题 [英] ToString Conversion Issue

查看:84
本文介绍了ToString转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我在调查对象'时遇到System.Single

类型之间转换的原因时遇到问题ToString方法。我正在把数据拉出来一个OdbcDataReader的
,它将其中一列声明为Single。


示例:


OdbcDataReader odbcReader = odbcCmd.ExecuteReader();


odbcReader.Read();


//此时的odbcReader有一行有一列,列是




//是1000.08008,系统类型是System.Single。我想将价值

投射到一个字符串中,并且精度为7,因此所需的结果应该是




//1000.0800800。


//我试过这个但它不起作用。


string s = odbcReader [0] .ToString()。ToString(" F7");


//现在s = 1000.0800000。它圆润了


//我也试过这个,是的,它没有给我正确的结果


String s = Convert.ToSingle (odbcReader [0])。ToString(" F7")

// s = 1000.0800000仍然四舍五入!!


为什么是ToString()舍入值?如何阻止它发生

并获得与odbcReader [0](1000.08008)中存储的完全相同的值。


干杯!


Hello,

I''m having problems figuring out why a conversion between a System.Single
type roundd when calling the object''s ToString method. I''m pulling data out
of an OdbcDataReader which declares one of the columns as type Single.

Example:

OdbcDataReader odbcReader = odbcCmd.ExecuteReader();

odbcReader.Read();

// At this point the odbcReader has one row with one column, the column
value

// is 1000.08008 and the system type is System.Single. I want the value to
be cast

// to a string and have precision of 7 therefore the desired result should
be

//1000.0800800.

//I tried this but it doesn''t work.

string s = odbcReader[0].ToString().ToString("F7");

//now s = 1000.0800000. it rounded

//I also tried this and yes it doesn''t give me the right results

String s = Convert.ToSingle(odbcReader[0]).ToString("F7")

//s = 1000.0800000 still rounding!!

Why is the ToString() rounding the value? How do I stop it from happening
and get exactly the same value that is stored in odbcReader[0] (1000.08008).

Cheers!


推荐答案

Neil,


我根本没有尝试过这个但是也许Convert.ToString会

给你更好的结果?

-

____________________

Klaus H. Probst ,MVP
http://www.vbbox.com/

" Neil Guyette" <纳克****** @ gemssensors.com>在消息中写道

news:eN ************** @ TK2MSFTNGP09.phx.gbl ...
Neil,

I haven''t experimented with this at all but perhaps Convert.ToString will
give you better results?
--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/
"Neil Guyette" <ng******@gemssensors.com> wrote in message
news:eN**************@TK2MSFTNGP09.phx.gbl...
你好,
我在调查对象的ToString方法时,为什么在System.Single
类型之间进行转换时遇到了问题。我从OdbcDataReader中提取数据
,它将其中一列声明为Single类型。

例如:

OdbcDataReader odbcReader = odbcCmd.ExecuteReader() ;

odbcReader.Read();

//此时odbcReader有一行有一列,列


//是1000.08008,系统类型是System.Single。我希望将
的值转换为字符串并且精度为7因此所需的结果应该是

//1000.0800800 。

//我尝试了这个但是它不起作用。

string s = odbcReader [0] .ToString()。ToString(" F7");

//现在s = 1000.0800000。它圆润了

//我也尝试了这个,是的,它没有给我正确的结果

String s = Convert.ToSingle(odbcReader [0])。ToString (F7)

// s = 1000.0800000仍然四舍五入!!

为什么ToString()舍入值?如何阻止它发生
并获得与odbcReader [0]
(1000.08008)中存储的完全相同的值。


干杯!

Hello,

I''m having problems figuring out why a conversion between a System.Single
type roundd when calling the object''s ToString method. I''m pulling data out of an OdbcDataReader which declares one of the columns as type Single.

Example:

OdbcDataReader odbcReader = odbcCmd.ExecuteReader();

odbcReader.Read();

// At this point the odbcReader has one row with one column, the column
value

// is 1000.08008 and the system type is System.Single. I want the value to be cast

// to a string and have precision of 7 therefore the desired result should
be

//1000.0800800.

//I tried this but it doesn''t work.

string s = odbcReader[0].ToString().ToString("F7");

//now s = 1000.0800000. it rounded

//I also tried this and yes it doesn''t give me the right results

String s = Convert.ToSingle(odbcReader[0]).ToString("F7")

//s = 1000.0800000 still rounding!!

Why is the ToString() rounding the value? How do I stop it from happening
and get exactly the same value that is stored in odbcReader[0] (1000.08008).


Cheers!




Neil,


我没有尝试过这个总之,但也许转换.ToString将

给你更好的结果?

-

____________________

Klaus H .Probst,MVP
http://www.vbbox.com/

" Neil Guyette" <纳克****** @ gemssensors.com>在消息中写道

news:eN ************** @ TK2MSFTNGP09.phx.gbl ...
Neil,

I haven''t experimented with this at all but perhaps Convert.ToString will
give you better results?
--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/
"Neil Guyette" <ng******@gemssensors.com> wrote in message
news:eN**************@TK2MSFTNGP09.phx.gbl...
你好,
我在调查对象的ToString方法时,为什么在System.Single
类型之间进行转换时遇到了问题。我从OdbcDataReader中提取数据
,它将其中一列声明为Single类型。

例如:

OdbcDataReader odbcReader = odbcCmd.ExecuteReader() ;

odbcReader.Read();

//此时odbcReader有一行有一列,列


//是1000.08008,系统类型是System.Single。我希望将
的值转换为字符串并且精度为7因此所需的结果应该是

//1000.0800800 。

//我尝试了这个但是它不起作用。

string s = odbcReader [0] .ToString()。ToString(" F7");

//现在s = 1000.0800000。它圆润了

//我也尝试了这个,是的,它没有给我正确的结果

String s = Convert.ToSingle(odbcReader [0])。ToString (F7)

// s = 1000.0800000仍然四舍五入!!

为什么ToString()舍入值?如何阻止它发生
并获得与odbcReader [0]
(1000.08008)中存储的完全相同的值。


干杯!

Hello,

I''m having problems figuring out why a conversion between a System.Single
type roundd when calling the object''s ToString method. I''m pulling data out of an OdbcDataReader which declares one of the columns as type Single.

Example:

OdbcDataReader odbcReader = odbcCmd.ExecuteReader();

odbcReader.Read();

// At this point the odbcReader has one row with one column, the column
value

// is 1000.08008 and the system type is System.Single. I want the value to be cast

// to a string and have precision of 7 therefore the desired result should
be

//1000.0800800.

//I tried this but it doesn''t work.

string s = odbcReader[0].ToString().ToString("F7");

//now s = 1000.0800000. it rounded

//I also tried this and yes it doesn''t give me the right results

String s = Convert.ToSingle(odbcReader[0]).ToString("F7")

//s = 1000.0800000 still rounding!!

Why is the ToString() rounding the value? How do I stop it from happening
and get exactly the same value that is stored in odbcReader[0] (1000.08008).


Cheers!




嗨Neil,


感谢您的帖子。我在我身边重现了这个问题。为了解决这个问题,我建议你在转换为

String之前将Single转换为Double,例如:


double val = odbcReader [0];

string s = val.ToString(" F5");


在此期间,我正在联系我们的开发团队检查这个问题。


祝你有愉快的一天!


问候,


HuangTM

Microsoft在线合作伙伴支持

MCSE / MCSD


安全! - www.microsoft.com/security

发布是按原样提供的。没有保证,也没有赋予任何权利。

Hi Neil,

Thanks for your post. I reproduce the problem on my side. To work around
the problem, I suggest you convert Single to Double before converting to
String, for example:

double val = odbcReader[0];
string s = val.ToString("F5");

In the meantime, I am contacting our Developer Team to check this issue.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


这篇关于ToString转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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