0

Is it possible to format a double, so he doesn't chance the text 2140.76 to 214076 but instead letting it be 2140.76?

I can't use ',' for the decimal numbers, since the entire text file that I'm reading are numbers using '.' for separating the decimals, 10000 records, every day, so ...

EDIT:

double natMB = 0;
boolean check = double.TryParse(splitline[8], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out natMB);
if (check == false)
{
   natMB = 0;
}
else
{
   natMB = natMB * 1024;
}

double intMB = 0;
boolean check2 = double.TryParse(splitline[9], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out intMB);
if (check2==false)
{
   intMB=0;
}
else
{
   intMB = intMB * 1024;
}

The 0 value is necessary since I need to enter these values in an SQL statement, and they need to show up as 0, not as null.

AgentFire
  • 8,944
  • 8
  • 43
  • 90
Dashzapp
  • 123
  • 4
  • 13
  • Do you know, that in some countries default separator for fraction part is comma, in other countries dot? This is called Culture Settings. – Kamil Mar 02 '12 at 08:08
  • I did not know about culture settings, but now I do! I'm still here to learn ... :s – Dashzapp Mar 02 '12 at 08:15
  • So be careful with number, date and time formats. They may be diffrent in systems with diffrent language. For example - in my country default fraction separator is ",". In USA there is 12-hour time format (and you can't simply sort strings with time represented like this). – Kamil Mar 02 '12 at 08:22
  • 1
    Show us sample of text that you trying to parse. Maybe there is better way. By the way - you can reduce your code. Use: if (double.TryParse(splitline[8], out natMB)) // instead of using variable. – Kamil Mar 02 '12 at 08:26
  • Added my way of doing it now, it should be doubles, the input is megabyte, the output is kilobyte (the 1024) – Dashzapp Mar 02 '12 at 08:35

4 Answers4

1

You can use the invariant culture to format a number with a decimal period, regardless of your local culture settings:

string formatted = someDouble.ToString(CultureInfo.InvariantCulture);
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

Your question is not clear, Do you want to parse a double from a string with dot decimal separator ?

If yes try with this :

double.Parse("2140.76", CultureInfo.InvariantCulture)
aleroot
  • 71,077
  • 30
  • 176
  • 213
0

Start reading here:
http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.aspx

Basically, you create an NumberFormatInfo that you can customise to use with String.Format to use any format you want.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
0

Is it possible to format a double so he doesn't chance the text 2140.76 to 214076 but instead letting it be 2140.76?

Yes. Let me play ignorant - I have no idea how you can even ask that and have the poroblem given the extensive formatting methods.

I can't use ',' for the decimal numbers, since the entire text file that i'm reading are numbers using '.' for separating the decimals, 10000 records, every day, so ...

So the problem likely is that you ahve a culture issue at hand and an ignorant developer on the other side. Ignorant because files exchagned should always be english formatted always, to avoid that.

Anyhow, basically: * Change your culture info in the thread to english or * Use english culture info for parsing and text generation.

Read the documentation for the methods you use -there areo verlaods that allow you to tune the formatting.

TomTom
  • 61,059
  • 10
  • 88
  • 148
  • "files exchagned should always be english formatted always", uhm shouldn't that be `InvariantCulture` formatted? – Christian.K Mar 02 '12 at 08:03
  • InvariantCulture yes, but that IS english mostly... and the problem is if you write a file for exahgne and tell someone it is InvariantCulture you may get a blank look as this is a .NET specific term. And you can not assume on file exchanges that the people know .NET terms ;) So, I prefer using english - so a Java or C++ programmer does not get me that blank look when the brain goes into google mode ;) – TomTom Mar 02 '12 at 08:05
  • 1
    I can sympathize with your reasoning, however (maybe because I'm not a US citizen ;-), I still find it difficult. For example, there are different "english" cultures (.NET or not) and there are even [differences](http://stackoverflow.com/a/2329317/21567) between "invariant" and `en-US` (which I assume you refer to) cultures. Notably the 24-hour vs. 12-hour format. In general I have learned to make every assumption in a file (interface) explicit. Including but not limited to number formats and especially about the character set allowed (but I digress from the OP's question now ;-) – Christian.K Mar 02 '12 at 08:12