473,671 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Double ToString format string

I'm trying to format a double with this behavior:

1) should only show 8 characters (character count before and after
decimal added together).
2) if greater than 7 characters to the left of the decimal, should
display in scientific notation.
3) if 7 characters or less to the left of the decimal, should round the
characters to the right of the decimal so only 8 characters are shown.

Anyone have any idea how to do this with the Double.ToString () format
string? Obviously using "e" as the format argument to ToString() will
always produce scientific notation, but that's not what I want in all
cases. Examples of what I'd like are:

orignal value desired value
------------- -------------
123.0 123.0
1234567.891 1234567.9
123456789.987 1.23e+08
123.456789123 123.45679

Thanks in advance for any help

--Ed
Nov 16 '05 #1
1 8926
Ed
Use the string method indexof with the decimal as an argument. This will tell you where the decimal is and then you can decide what to do from there, if the indexof returns 9 or greater then you need to convert to sci not, but if index of is 8 or less then you need to do your rounding algo.
Nov 16 '05 #2

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

Similar topics

4
9746
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a numeric value according to an arbitrary regular expression.
2
9441
by: Rod Brick | last post by:
I'm trying to print a Double in straight decimal form, not exponential. I can't seem to accomplish this. This seems like it should be simple enough. The output I'm looking for is "0.00001", not "1E-05". I'm obviously baffled by the NumberFormatInfo class, I can't seem to make it do what I want. And, I've tried the following, all of which have failed to modify the original exponential form. The commented out "D7" statement throws a...
3
12425
by: Boz | last post by:
Hi, I am trying to use string.Format() to output the value of a double. double da = 100000000000.99994; double db = 100000000000.9994; double dc = 100000000000.994; double dd = 1000000000.99994; Debug.WriteLine(string.Format("a={0:F10}", da));
5
5646
by: Markus Kling | last post by:
"double.Parse(double.MaxValue.ToString())" yields the following Exception: Value was either too large or too small for a Double. at System.Number.ParseDouble(String value, NumberStyles options, NumberFormat Info numfmt) at System.Double.Parse(String s, NumberStyles style, NumberFormatInfo info) at System.Double.Parse(String s) ...
9
2083
by: Coleen | last post by:
Hi All :-) I found the way to get my column sum (Thanks Cor I did it a little different, but the result is what I wanted) I used: dt_stat_report_3b.Columns.Add(New DataColumn("Sum", GetType(Double), "sum(Column_10_ld_act_125_gtr_fy_fy_hh_avg)")) where dt_stat_report_3b is my datatable, Sum is my column name and sum(Column_10_ld_act_125_gtr_fy_fy_hh_avg) is the sum of the column I needed. This Does return me the correct value in the...
12
2687
by: ThunderMusic | last post by:
Hi, We have a part of our application that deals with millions of records and do some processing of them. We've achieved a pretty good performance gain by developping a custom DateTime.ToString and a custom int.ToString, but we can't find any clue on doing for decimal and double, which would be about half the load if they are put together (decimal and double). By changing our DateTime and Int ToStrings, we achieved a 84% performance gain,...
3
3844
by: =?Utf-8?B?U2NvdHQ=?= | last post by:
Hello, I would like to convert a double to a string and have the resulting string contain all of the original values after the decimal place. Currently, the problem that I am having is that the new string value is being rounded to nine decimal places. Dim dblX As Double = 146785.62713610753 dim strText as String = dblX.ToString()
1
3085
by: Lee | last post by:
Hi, I am trying to convert a double to a string using the following format;- double = 1.01 string = 01.0100 or
2
1332
by: Derek Hart | last post by:
I have a double stored in a DataTable: dt.Rows(i)(MergeFieldName) I want to format this and store it into a string. I have formatting stored in a database, such as "$#,##0.00"
2
4627
by: Lior Bobrov | last post by:
Hi ... How to convert a variable of type Double to String , *preserving* the original value of the Double variable , as is , in a short (convenient) way ? For example , if there are two variables : Dim dblResult as Double Dim strResult as String
0
8473
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
8390
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
8911
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
8819
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
8597
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
7428
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
4222
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...
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1806
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.