473,763 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Roun d) float's to 7 decimals, double's to 15
and decimals to 28 decimals, but that does not help.
And the System.Globaliz ation.NumberFor matInfo class does not seem to provide
a such function.
Thank you

Regards, Dennis
Nov 15 '05 #1
5 17662
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.Threadin g;
using System.Globaliz ation;

class Class1
{
static void Main()
{
Thread.CurrentT hread.CurrentCu lture = new CultureInfo("en-us");
double MyDouble = 123456789;

Console.WriteLi ne("The examples in en-US culture.\n");
Console.WriteLi ne(MyDouble.ToS tring("C"));
Console.WriteLi ne(MyDouble.ToS tring("E"));
Console.WriteLi ne(MyDouble.ToS tring("P"));
Console.WriteLi ne(MyDouble.ToS tring("N"));
Console.WriteLi ne(MyDouble.ToS tring("F"));

Thread.CurrentT hread.CurrentCu lture = new CultureInfo("de-DE");
Console.WriteLi ne("The examples in de-DE culture.\n");
Console.WriteLi ne(MyDouble.ToS tring("C"));
Console.WriteLi ne(MyDouble.ToS tring("E"));
Console.WriteLi ne(MyDouble.ToS tring("P"));
Console.WriteLi ne(MyDouble.ToS tring("N"));
Console.WriteLi ne(MyDouble.ToS tring("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**********@g reydigital.no-spam.no> wrote in message
news:bl******** **@news.tdcnorg e.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.Roun d) float's to 7 decimals, double's to 15
and decimals to 28 decimals, but that does not help.
And the System.Globaliz ation.NumberFor matInfo 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.c om> wrote
in message news:O$******** *****@TK2MSFTNG P12.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.Threadin g;
using System.Globaliz ation;

class Class1
{
static void Main()
{
Thread.CurrentT hread.CurrentCu lture = new CultureInfo("en-us");
double MyDouble = 123456789;

Console.WriteLi ne("The examples in en-US culture.\n");
Console.WriteLi ne(MyDouble.ToS tring("C"));
Console.WriteLi ne(MyDouble.ToS tring("E"));
Console.WriteLi ne(MyDouble.ToS tring("P"));
Console.WriteLi ne(MyDouble.ToS tring("N"));
Console.WriteLi ne(MyDouble.ToS tring("F"));

Thread.CurrentT hread.CurrentCu lture = new CultureInfo("de-DE");
Console.WriteLi ne("The examples in de-DE culture.\n");
Console.WriteLi ne(MyDouble.ToS tring("C"));
Console.WriteLi ne(MyDouble.ToS tring("E"));
Console.WriteLi ne(MyDouble.ToS tring("P"));
Console.WriteLi ne(MyDouble.ToS tring("N"));
Console.WriteLi ne(MyDouble.ToS tring("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**********@g reydigital.no-spam.no> wrote in message
news:bl******** **@news.tdcnorg e.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.Roun d) float's to 7 decimals, double's to 15
and decimals to 28 decimals, but that does not help.
And the System.Globaliz ation.NumberFor matInfo 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**********@g reydigital.no-spam.no> wrote in message
news:bl******** **@news.tdcnorg e.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.c om> wrote
in message news:O$******** *****@TK2MSFTNG P12.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.Threadin g;
using System.Globaliz ation;

class Class1
{
static void Main()
{
Thread.CurrentT hread.CurrentCu lture = new CultureInfo("en-us");
double MyDouble = 123456789;

Console.WriteLi ne("The examples in en-US culture.\n");
Console.WriteLi ne(MyDouble.ToS tring("C"));
Console.WriteLi ne(MyDouble.ToS tring("E"));
Console.WriteLi ne(MyDouble.ToS tring("P"));
Console.WriteLi ne(MyDouble.ToS tring("N"));
Console.WriteLi ne(MyDouble.ToS tring("F"));

Thread.CurrentT hread.CurrentCu lture = new CultureInfo("de-DE");
Console.WriteLi ne("The examples in de-DE culture.\n");
Console.WriteLi ne(MyDouble.ToS tring("C"));
Console.WriteLi ne(MyDouble.ToS tring("E"));
Console.WriteLi ne(MyDouble.ToS tring("P"));
Console.WriteLi ne(MyDouble.ToS tring("N"));
Console.WriteLi ne(MyDouble.ToS tring("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**********@g reydigital.no-spam.no> wrote in message
news:bl******** **@news.tdcnorg e.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.Roun d) float's to 7 decimals, double's to 15 and decimals to 28 decimals, but that does not help.
And the System.Globaliz ation.NumberFor matInfo class does not seem to

provide
a such function.
Thank you

Regards, Dennis



Nov 15 '05 #4
Dennis Myrén <de**********@g reydigital.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.co m>
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**********@g reydigital.no-spam.no> wrote in message
news:bl******** **@news.tdcnorg e.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.c om> wrote
in message news:O$******** *****@TK2MSFTNG P12.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.Threadin g;
using System.Globaliz ation;

class Class1
{
static void Main()
{
Thread.CurrentT hread.CurrentCu lture = new CultureInfo("en-us");
double MyDouble = 123456789;

Console.WriteLi ne("The examples in en-US culture.\n");
Console.WriteLi ne(MyDouble.ToS tring("C"));
Console.WriteLi ne(MyDouble.ToS tring("E"));
Console.WriteLi ne(MyDouble.ToS tring("P"));
Console.WriteLi ne(MyDouble.ToS tring("N"));
Console.WriteLi ne(MyDouble.ToS tring("F"));

Thread.CurrentT hread.CurrentCu lture = new CultureInfo("de-DE");
Console.WriteLi ne("The examples in de-DE culture.\n");
Console.WriteLi ne(MyDouble.ToS tring("C"));
Console.WriteLi ne(MyDouble.ToS tring("E"));
Console.WriteLi ne(MyDouble.ToS tring("P"));
Console.WriteLi ne(MyDouble.ToS tring("N"));
Console.WriteLi ne(MyDouble.ToS tring("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**********@g reydigital.no-spam.no> wrote in message
news:bl******** **@news.tdcnorg e.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.Roun d) float's to 7 decimals, double's to 15 and decimals to 28 decimals, but that does not help.
And the System.Globaliz ation.NumberFor matInfo 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
2155
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 they are. I tried to use tostring("f") but it needs to have a precision number after *f* which will cause always have fix number of characters (for example if use f10 then 0.09 is converted to 0.09000000 while I need to show it as 0.09). do...
1
8865
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, and then apply an XSLT to transform the XML into HTML. This works great (or did) until today. I just found out that the "number-format" command in XSLT can't handle scientific notation! So when I try to format these numbers I just get "NaN" on...
0
1712
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 numbers are defined as numeric in the SqlServer Database and are bound to text boxes in the app. In the dataset xml these numbers are defined as decimal. I am using the binding method Format to display the data in scientific notation and Parse to...
7
10917
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
7266
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
6669
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 // returns 503.51000000000005, rounding error! 4.98e2 + 5.51 // returns 503.51, correct!
2
2766
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 decimal values are written out in scientific notation ("9.984348392E+08"). This wouldn't be a problem except when I try to READ the XML file back into a DataSet I keep getting a System.FormatException saying "Input string was not in a correct format."...
2
3487
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 a way that no leading 00 are inserted for the exponent, so the above would look like this:
2
7032
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 numbers are defined as numeric in the SqlServer Database and are bound to text boxes in the app. In the dataset's xml these numbers are defined as decimal. I am using the binding method Format to display the data in scientific notation and Parse to...
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10145
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
9998
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
9938
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
8822
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...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
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
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.