为什么我不能将字符串值转换为十进制/浮点数 [英] Why Can't I Convert String Value To Decimal / Float

查看:108
本文介绍了为什么我不能将字符串值转换为十进制/浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个响应Json对象,我将其反序列化为



var v = JsonConvert.DeserializeObject< list>< currentvalue>>(CurrentContent);



现在所需的值在v [0] .l_cur //200.00



string Value = v [0] .l_cur.ToString()修剪();



我需要将此转换为十进制值



i尝试

< br $>
var CurrentStockValue = Convert.ToDecimal(Value);



var CurrentStockValue = Decimal.Parse(Value,NumberStyle.AllowDecimalPoint,CultureInfo.Invarient) ;



我收到此错误



输入字符串格式不正确。



任何人都可以指导我完成这个..

I get a response Json object which i deserialize like

var v = JsonConvert.DeserializeObject<list><currentvalue>>(CurrentContent);

now the required value is in v[0].l_cur // "200.00"

string Value = v[0].l_cur.ToString().Trim();

I need to convert this to decimal Value

i tried

var CurrentStockValue = Convert.ToDecimal(Value);

var CurrentStockValue = Decimal.Parse(Value, NumberStyle.AllowDecimalPoint , CultureInfo.Invarient);

I get this error

"Input string was not in a correct format."

Can any one guide me through this..

推荐答案

首先查看你的数据:使用调试器在转换尝试之前检查Value的内容。

我猜它根本不包含200.0 - 但是如果不知道其中究竟是什么,那么每个人都在猜测。

当你知道它是什么时,看看它来自哪里:v [0] .l_cur,看看它包含了什么。



在某些时候,显然数据是无效的,然后它应该相对简单勒解决原因。但是在你知道确切的数据和问题是什么之前,你无法解决任何问题!
Start by looking at your data: use the debugger to examine the content of Value immediately prior to the conversion attempt.
I'm guessing it doesn't contain "200.0" at all - but without knowing exactly what is in it, everybody is just guessing.
When you know what is in that, look at where is came from: v[0].l_cur, and see what that contains.

At some point it will be obvious that the data is invalid, and then it should be relatively simple to work out why. But until you know what the exact data and problem is, you can't fix anything!


这篇关于为什么我不能将字符串值转换为十进制/浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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