Avoid scientific notation when casting numbers to string

D

Dennis Myrén

Hi.

Is there a way to make sure that float, double and decimal data types
never will be presented in a scientific notation?

I have tried to round(Math.Round) float's to 7 decimals, double's to 15
and decimals to 28 decimals, but that does not help.
And the System.Globalization.NumberFormatInfo class does not seem to provide
a such function.


Thank you

Regards, Dennis
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi,

You could just play with format specification strings like in this MSDN
example from the
"Standard Numeric Format Strings" topic:

using System;
using System.Threading;
using System.Globalization;

class Class1
{
static void Main()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
double MyDouble = 123456789;

Console.WriteLine("The examples in en-US culture.\n");
Console.WriteLine(MyDouble.ToString("C"));
Console.WriteLine(MyDouble.ToString("E"));
Console.WriteLine(MyDouble.ToString("P"));
Console.WriteLine(MyDouble.ToString("N"));
Console.WriteLine(MyDouble.ToString("F"));

Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
Console.WriteLine("The examples in de-DE culture.\n");
Console.WriteLine(MyDouble.ToString("C"));
Console.WriteLine(MyDouble.ToString("E"));
Console.WriteLine(MyDouble.ToString("P"));
Console.WriteLine(MyDouble.ToString("N"));
Console.WriteLine(MyDouble.ToString("F"));
}
}
 
D

Dennis Myrén

Thank you for the reply.
However, it is not that simple.
Not any of those format strings does help in my case.


Dmitriy Lapshin said:
Hi,

You could just play with format specification strings like in this MSDN
example from the
"Standard Numeric Format Strings" topic:

using System;
using System.Threading;
using System.Globalization;

class Class1
{
static void Main()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
double MyDouble = 123456789;

Console.WriteLine("The examples in en-US culture.\n");
Console.WriteLine(MyDouble.ToString("C"));
Console.WriteLine(MyDouble.ToString("E"));
Console.WriteLine(MyDouble.ToString("P"));
Console.WriteLine(MyDouble.ToString("N"));
Console.WriteLine(MyDouble.ToString("F"));

Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
Console.WriteLine("The examples in de-DE culture.\n");
Console.WriteLine(MyDouble.ToString("C"));
Console.WriteLine(MyDouble.ToString("E"));
Console.WriteLine(MyDouble.ToString("P"));
Console.WriteLine(MyDouble.ToString("N"));
Console.WriteLine(MyDouble.ToString("F"));
}
}


--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Dennis Myr?n said:
Hi.

Is there a way to make sure that float, double and decimal data types
never will be presented in a scientific notation?

I have tried to round(Math.Round) float's to 7 decimals, double's to 15
and decimals to 28 decimals, but that does not help.
And the System.Globalization.NumberFormatInfo class does not seem to provide
a such function.


Thank you

Regards, Dennis
 
D

Dmitriy Lapshin [C# / .NET MVP]

Dennis,

Maybe it's just I don't understand what you are trying to achieve? Could you
please elaborate?

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Dennis Myr?n said:
Thank you for the reply.
However, it is not that simple.
Not any of those format strings does help in my case.


Dmitriy Lapshin said:
Hi,

You could just play with format specification strings like in this MSDN
example from the
"Standard Numeric Format Strings" topic:

using System;
using System.Threading;
using System.Globalization;

class Class1
{
static void Main()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
double MyDouble = 123456789;

Console.WriteLine("The examples in en-US culture.\n");
Console.WriteLine(MyDouble.ToString("C"));
Console.WriteLine(MyDouble.ToString("E"));
Console.WriteLine(MyDouble.ToString("P"));
Console.WriteLine(MyDouble.ToString("N"));
Console.WriteLine(MyDouble.ToString("F"));

Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
Console.WriteLine("The examples in de-DE culture.\n");
Console.WriteLine(MyDouble.ToString("C"));
Console.WriteLine(MyDouble.ToString("E"));
Console.WriteLine(MyDouble.ToString("P"));
Console.WriteLine(MyDouble.ToString("N"));
Console.WriteLine(MyDouble.ToString("F"));
}
}


--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Dennis Myr?n said:
Hi.

Is there a way to make sure that float, double and decimal data types
never will be presented in a scientific notation?

I have tried to round(Math.Round) float's to 7 decimals, double's to 15
and decimals to 28 decimals, but that does not help.
And the System.Globalization.NumberFormatInfo class does not seem to provide
a such function.


Thank you

Regards, Dennis
 
J

Jon Skeet

Dennis Myrén said:
Thank you for the reply.
However, it is not that simple.
Not any of those format strings does help in my case.

The "F" specifier should be fine for you, but you'll need to specify
how many decimal places you want.

Note that .NET doesn't give you any easy way of formatting the *exact*
value of a double (or float) as a string. If you want that, you can use
the code I've got on
http://www.pobox.com/~skeet/csharp/floatingpoint.html
 
D

Dennis Myrén

Thank you all for your help.
I just didnt realize i could specify the number of decimals as well, when
using the "f"
format string.

Now it works.

Regards, Dennis


Dennis Myrén said:
Thank you for the reply.
However, it is not that simple.
Not any of those format strings does help in my case.


Dmitriy Lapshin said:
Hi,

You could just play with format specification strings like in this MSDN
example from the
"Standard Numeric Format Strings" topic:

using System;
using System.Threading;
using System.Globalization;

class Class1
{
static void Main()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
double MyDouble = 123456789;

Console.WriteLine("The examples in en-US culture.\n");
Console.WriteLine(MyDouble.ToString("C"));
Console.WriteLine(MyDouble.ToString("E"));
Console.WriteLine(MyDouble.ToString("P"));
Console.WriteLine(MyDouble.ToString("N"));
Console.WriteLine(MyDouble.ToString("F"));

Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
Console.WriteLine("The examples in de-DE culture.\n");
Console.WriteLine(MyDouble.ToString("C"));
Console.WriteLine(MyDouble.ToString("E"));
Console.WriteLine(MyDouble.ToString("P"));
Console.WriteLine(MyDouble.ToString("N"));
Console.WriteLine(MyDouble.ToString("F"));
}
}


--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Dennis Myr?n said:
Hi.

Is there a way to make sure that float, double and decimal data types
never will be presented in a scientific notation?

I have tried to round(Math.Round) float's to 7 decimals, double's to 15
and decimals to 28 decimals, but that does not help.
And the System.Globalization.NumberFormatInfo class does not seem to provide
a such function.


Thank you

Regards, Dennis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top