473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Avoid scientific notation when casting numbers to string

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
Nov 15 '05 #1
5 17608
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" <de**********@greydigital.no-spam.no> wrote in message
news:bl**********@news.tdcnorge.no...
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


Nov 15 '05 #2
Thank you for the reply.
However, it is not that simple.
Not any of those format strings does help in my case.
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:O$*************@TK2MSFTNGP12.phx.gbl...
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" <de**********@greydigital.no-spam.no> wrote in message
news:bl**********@news.tdcnorge.no...
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

Nov 15 '05 #3
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" <de**********@greydigital.no-spam.no> wrote in message
news:bl**********@news.tdcnorge.no...
Thank you for the reply.
However, it is not that simple.
Not any of those format strings does help in my case.
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:O$*************@TK2MSFTNGP12.phx.gbl...
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" <de**********@greydigital.no-spam.no> wrote in message
news:bl**********@news.tdcnorge.no...
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



Nov 15 '05 #4
Dennis Myrén <de**********@greydigital.no-spam.no> wrote:
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
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

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" <de**********@greydigital.no-spam.no> wrote in message
news:bl**********@news.tdcnorge.no...
Thank you for the reply.
However, it is not that simple.
Not any of those format strings does help in my case.
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:O$*************@TK2MSFTNGP12.phx.gbl...
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" <de**********@greydigital.no-spam.no> wrote in message
news:bl**********@news.tdcnorge.no...
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


Nov 15 '05 #6

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

Similar topics

1
by: masoud bayan | last post by:
I have some values in type of double such 0.00009 , 0.0000007, when I want to show them in a text box and convert them to string they are changed to scientific notation while I want to show them as...
1
by: Nick | last post by:
Well, the project I am working on has now come to a screeching halt! I have been developing a program that heavily utilizes ADO.NET record sets. To generate reports, I convert the recordset to XML,...
0
by: Greg | last post by:
I am working on an application that requires working with numbers in scientific notation. I am using SqlServer as the database and I have created strongly typed data adapters and datasets. The...
7
by: Dustan | last post by:
How can I get a number into scientific notation? I have a preference for the format '1 E 50' (as an example), but if it's well known, it works.
2
by: Ryan Liu | last post by:
In C#, for a large float (9 digitals), how can I disable Scientific notation. When it auto convert to Scientific notation, I lost accuracy. Thanks a lot! Ryan
9
by: Joe Attardi | last post by:
Hi all, Math is not my strongest area so forgive me if I use some of the wrong terminology. It seems that scientific notation is immune to rounding errors. For example: (4.98 * 100) + 5.51 ...
2
by: rSmoke | last post by:
I have a DataSet that contains a table with about 6 columns of high accuracy decimal values. When I try to write out the DataSet using the WriteXML() function the XML is written fine, but the...
2
by: Robert Ludig | last post by:
When I format a number to a string in scientific notation: string.Format("{0:E}",20000); I get the following string (culture de-DE): 2,000000E+003 How can I format scientific notation in...
2
by: Greg | last post by:
I am working on an application that requires working with numbers in scientific notation. I am using SqlServer as the database and I have created strongly typed data adapters and datasets. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.