471,326 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,326 software developers and data experts.

how to convert this

got a string a="2.3944864845405107E+18"

how to convert this into a valid number. which method to use..
I was using Convert.ToInt64(a); which is going to exception.
thanks
Praveen
Jun 27 '08 #1
5 1346
try decimal.Parse() instead
Jun 27 '08 #2
Praveen skrev:
got a string a="2.3944864845405107E+18"

how to convert this into a valid number. which method to use..
I was using Convert.ToInt64(a); which is going to exception.

First: The string you have is not representing an integer, so you have
to go through a string to double step first.

Secondly: Converting a string to a double is language dependent, - in
German, Norwegian and French as an example the expected decimal
separator is a comma ',', instead of a period (.).

If you know that the string always is using '.' as decimal point you
should parse it by using CultureInfo.InvariantCulture to avoid a crash
if running on a German OS.

Int64 value = Convert.ToInt64(Convert.ToDouble(a,
System.Globalization.CultureInfo.InvariantCulture) );

--
Bjorn Brox
Jun 27 '08 #3
>got a string a="2.3944864845405107E+18"
Secondly: Converting a string to a double is language dependent, - in
German, Norwegian and French as an example the expected decimal
separator is a comma ',', instead of a period (.).
I'm curious as to what will happen if you try converting this on a German
locale as it is a number in standard form and and wonder if it is an
exception or not. Could you try?
Jun 27 '08 #4
Peter Morris wrote:
>>got a string a="2.3944864845405107E+18"
Secondly: Converting a string to a double is language dependent, - in
German, Norwegian and French as an example the expected decimal
separator is a comma ',', instead of a period (.).

I'm curious as to what will happen if you try converting this on a German
locale as it is a number in standard form and and wonder if it is an
exception or not. Could you try?
You can try it yourself just specifying a german culture as second
argument.

Arne
Jun 27 '08 #5
Peter Morris skrev:
>>got a string a="2.3944864845405107E+18"
>Secondly: Converting a string to a double is language dependent, - in
German, Norwegian and French as an example the expected decimal
separator is a comma ',', instead of a period (.).

I'm curious as to what will happen if you try converting this on a German
locale as it is a number in standard form and and wonder if it is an
exception or not. Could you try?

Convert.ToDouble(a) throw a System.FormatException

--
Bjorn Brox
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Logan X via .NET 247 | last post: by
3 posts views Thread by Convert TextBox.Text to Int32 Problem | last post: by
7 posts views Thread by patang | last post: by
2 posts views Thread by SimonZ | last post: by
17 posts views Thread by Terry Jolly | last post: by
1 post views Thread by Wes Peters | last post: by
4 posts views Thread by tshad | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.