3 Replies - 101762 Views - Last Post: 19 April 2010 - 12:23 AM Rate Topic: -----

#1 tayeen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-December 06

Converting string to float in C#

Posted 21 December 2006 - 11:15 PM

I have a string which represents a floating-point number. When i try to convert it to float number i don't get the exact floating point number. my example code is given below.

string s_num = "666890.23"
float f_num = float.Parse(s_num);


The result is f_num = 666890.25

Why is this happening. Why am I not getting the exact figure.
Please help me out.

Is This A Good Question/Topic? 0
  • +

Replies To: Converting string to float in C#

#2 shakti8ie  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 11-December 06

Re: Converting string to float in C#

Posted 22 December 2006 - 12:13 AM

use System.Convert.ToDouble(s_num) and change float to double...
Was This Post Helpful? 1

#18 unreal_origin  Icon User is offline

  • Wisdom Listens
  • member icon

Reputation: 4
  • View blog
  • Posts: 742
  • Joined: 21-April 05

Re: Converting string to float in C#

Posted 24 December 2006 - 12:38 PM

according to this website floats in C# are only Precise to 7 digits
C# Float

if you were to do something like
string s_num = "66690.23"
float f_num = float.Parse(s_num);

The result is f_num = 66690.23
it is better to just use double. Less problems

double.Parse(s_num);

hope that helps

This post has been edited by unreal_origin: 24 December 2006 - 12:50 PM

Was This Post Helpful? 1

#19 Guest_shift*


Reputation:

Re: Converting string to float in C#

Posted 19 April 2010 - 12:23 AM

thanx it worked
Was This Post Helpful? 0

Page 1 of 1