473,760 Members | 10,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I parse a string number into a float with four decimal places?

OK this should be bread and butter, easy to do, but I seem to be going
around in circles and not getting any answer to achieving this simple
task.

I have numbers in string format (they are coming in from an excel
sheet):
Examples.
45,666.0041
5664456.12
-5465.25568 ETC

I need to format this into a specified number of decimal places without
affecting anything else.

When I require 2 decimal places, this seams to work:

this.plotPoint =
float.Parse(Str ing.Format("{0: F2}",row[fundReturnColum n]));

BUT when I want 4 decmail places:

this.plotPoint =
float.Parse(Str ing.Format("{0: F4}",row[fundReturnColum n]));

I still get 2??????
I am sure that there is a simple explanation, but its driving me mad.}

Feb 22 '06 #1
4 9492
I think that your input strings specify too high a precision to fit a float.
With a single-precision float you could get at most 7 significant digits but,
for example, your first string has 9 significant digits. So I would suggest to
try parsing them into doubles instead.

HTH,
-rick-

Phil Mc wrote:
OK this should be bread and butter, easy to do, but I seem to be going
around in circles and not getting any answer to achieving this simple
task.

I have numbers in string format (they are coming in from an excel
sheet):
Examples.
45,666.0041
5664456.12
-5465.25568 ETC

I need to format this into a specified number of decimal places without
affecting anything else.

When I require 2 decimal places, this seams to work:

this.plotPoint =
float.Parse(Str ing.Format("{0: F2}",row[fundReturnColum n]));

BUT when I want 4 decmail places:

this.plotPoint =
float.Parse(Str ing.Format("{0: F4}",row[fundReturnColum n]));

I still get 2??????
I am sure that there is a simple explanation, but its driving me mad.}

Feb 22 '06 #2
"Phil Mc" <ph**********@p ramerica.ie> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
this.plotPoint =
float.Parse(Str ing.Format("{0: F4}",row[fundReturnColum n]));


this.plotPoint = float.Parse(row[fundReturnColum n]).ToString("#,# #0.0000");
Feb 22 '06 #3
Hi thanks for the input.

Unfortunately I'm getting a error on ToString with only one argument

":No overload for method 'ToString' takes '1' arguments"

The way that I have got around this is to simply parse the string and
not convert to a number. I then use the string number in my sql
statement.
string numStr = String.Format(r ow[plotPointColumn].ToString().Tri m());
numStr = String.Format(" {0:0.0000}", numStr).Replace (",", "");

Not ideal, but getting the job done.

I would like to get to the bottom of the problem if anyone has any
ideas.
Thanks again for your help....

Feb 22 '06 #4
I'm certainly not sure why unless it has to do with the data type of
"this.plotPoint ", which you do not disclose. The following works for me on .Net
1.1 - is it not pretty close to what you are trying to do?

using System;

namespace ConsoleApplicat ion1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
double d1, d2, d3;
string s1 = "45,666.004 1";
string s2 = "5664456.12 ";
string s3 = "-5465.25568";
d1 = double.Parse(s1 );
d2 = double.Parse(s2 );
d3 = double.Parse(s3 );
Console.WriteLi ne("{0}, {1}, {2}", new object[]{f1.ToString("# ,0.0000"),

f2.ToString("#, 0.0000"), f3.ToString("#, 0.0000")});
Console.ReadLin e();
}
}
}

Note that this does not work so well if you change "double" to "float" . . .

-rick-

Phil Mc wrote:
Hi thanks for the input.

Unfortunately I'm getting a error on ToString with only one argument

":No overload for method 'ToString' takes '1' arguments"

The way that I have got around this is to simply parse the string and
not convert to a number. I then use the string number in my sql
statement.
string numStr = String.Format(r ow[plotPointColumn].ToString().Tri m());
numStr = String.Format(" {0:0.0000}", numStr).Replace (",", "");

Not ideal, but getting the job done.

I would like to get to the bottom of the problem if anyone has any
ideas.
Thanks again for your help....

Feb 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
7997
by: Bryan R. Meyer | last post by:
I am a relatively new C++ programmer and am attempting to write a function that takes a number of type float and adds commas to it in the appropriate places. In order to manipulate the number to add the commas, I convert it to a string (specifically a char rather than an actual string object) using the gcvt function as shown below. char amt; gcvt(amount,50,amt);
2
64488
by: Chi Tang | last post by:
Hi, I try to convert a string to a float but it alway comes out with extra value. For example, the string input is '12.6' but the output is '12.6000003814697' The following is my code to do the convert: float fBalance = (float)System.Convert.ToSingle("12.6"); Should I specify a format string for the convert? Thanks for any help, CT
2
12602
by: ngn | last post by:
How do you format the number of decimal places in a floating point number converted to a string? I simply use the ToString() method on the float variable, but I cannot find how to format it a #.## format (only 2 places of decimal). float n = 0.12348528f string str = n.ToString(); // ??? How do I now format str ???
8
7139
by: LW Irving | last post by:
when I use the snippet below price = "$" + reader.ToString() + " to $" + reader.ToString(); the price is returned with 4 decimal places I thought I could do; reader.ToString("C") to format the string as currency and do away "$" + from the line
6
7609
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards, Karthi
19
3610
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm starting a new one] I'd still like to finish this rounding mess. As a startup lemma we can take that VK is the worst programmer of all times and places: let's move from here forward please. The usability of any program depends on exact behavior...
28
5931
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? ----------------------------------------------------------------------- When formatting money for example, to format 6.57634 to 6.58, 6.5 to 6.50, and 6 to 6.00? Rounding of x.xx5 is uncertain, as such numbers are not represented exactly. See section 4.7 for Rounding issues.
10
3244
by: Hank Stalica | last post by:
I'm having this weird problem where my code does the following conversion from string to float: 27000000.0 -27000000.00 2973999.99 -29740000.00 2989999.13 -2989999.25 The number on the left is the string I get after tokenizing a bigger string. The number on the right is the number I get after the conversion.
20
5009
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
0
9333
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10107
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9945
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9900
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8768
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7324
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5361
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.