473,395 Members | 1,488 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,395 software developers and data experts.

StreamWriter: How to format a decimal?

Hi All,

This has to be simple, I feel foolish asking, but...

How do you format a decimal with a StreamWriter? I would like to write a
decimal value to a text file in the format xxxxx.xxxx (like 2 would be
2.0000 - don't need to prepend zeros).

Never thought I would say it, but I'm starting to miss printf().

David

Nov 15 '05 #1
3 8299
To answer my own question, I can use ToString()...

double x = 0.000;
writer.WriteLine("{0,-10}", x.ToString("#####.0000"));
writer.WriteLine("{0,10}", 77.ToString("#####.0000"));

But... This is kinda cumbersome. Is there a better way?

David
"David Rose" <Da********@rcn.com> wrote in message
news:uJ**************@TK2MSFTNGP10.phx.gbl...
Hi All,

This has to be simple, I feel foolish asking, but...

How do you format a decimal with a StreamWriter? I would like to write a
decimal value to a text file in the format xxxxx.xxxx (like 2 would be
2.0000 - don't need to prepend zeros).

Never thought I would say it, but I'm starting to miss printf().

David

Nov 15 '05 #2
Hi, David Rose,

You were quite close. Try:

writer.Write("{0:#####.0000}", x);

Greetings
Martin
"David Rose" <Da********@rcn.com> wrote in message
news:ej**************@TK2MSFTNGP12.phx.gbl...
To answer my own question, I can use ToString()...

double x = 0.000;
writer.WriteLine("{0,-10}", x.ToString("#####.0000"));
writer.WriteLine("{0,10}", 77.ToString("#####.0000"));

But... This is kinda cumbersome. Is there a better way?

David
"David Rose" <Da********@rcn.com> wrote in message
news:uJ**************@TK2MSFTNGP10.phx.gbl...
Hi All,

This has to be simple, I feel foolish asking, but...

How do you format a decimal with a StreamWriter? I would like to write a decimal value to a text file in the format xxxxx.xxxx (like 2 would be
2.0000 - don't need to prepend zeros).

Never thought I would say it, but I'm starting to miss printf().

David


Nov 15 '05 #3
Martin,

Thank you. That's what I was looking for!

David
"Martin Dechev" <de******************@hotmail.com> wrote in message
news:uO**************@TK2MSFTNGP11.phx.gbl...
Hi, David Rose,

You were quite close. Try:

writer.Write("{0:#####.0000}", x);

Greetings
Martin
"David Rose" <Da********@rcn.com> wrote in message
news:ej**************@TK2MSFTNGP12.phx.gbl...
To answer my own question, I can use ToString()...

double x = 0.000;
writer.WriteLine("{0,-10}", x.ToString("#####.0000"));
writer.WriteLine("{0,10}", 77.ToString("#####.0000"));

But... This is kinda cumbersome. Is there a better way?

David
"David Rose" <Da********@rcn.com> wrote in message
news:uJ**************@TK2MSFTNGP10.phx.gbl...
Hi All,

This has to be simple, I feel foolish asking, but...

How do you format a decimal with a StreamWriter? I would like to
write
a decimal value to a text file in the format xxxxx.xxxx (like 2 would be
2.0000 - don't need to prepend zeros).

Never thought I would say it, but I'm starting to miss printf().

David



Nov 15 '05 #4

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

Similar topics

6
by: J | last post by:
Would anyone know if there a type tag to format a double? I have f for floating point, but cannot find one for double.
3
by: stevek | last post by:
How do I format an integer. Add commas. 1234565 1,234,565 TIA
1
by: Mike MacSween | last post by:
This looks like a bug to me. I have an expression on a report: =Format(Sum((**)*(/)),"0.00") Score is byte PercentOfGrade is double PropDegree is single ModuleCats is byte
7
by: Alpha | last post by:
Hi, I'm maintaining C# code and am fairly new with C# programming. I'm looking for codes that's droping the 2nd digit of a nuber printed out and I suspect it's the code below. Can someone tell me...
3
by: Slonocode | last post by:
I have some textboxes bound to an access db. I wanted to format the textboxes that displayed currency and date info so I did the following: Dim WithEvents oBidAmt As Binding oBidAmt = New...
2
by: gordon | last post by:
Hi, I am trying to create a reference to an external file to write to using stream writer and a value that is created in a for. Basically the value that a user will create will be the name...
11
by: shsandeep | last post by:
I used the following query to retrieve the date in dd-mon-yyyy format. db2 => SELECT RTRIM(CHAR(DAY(COVG_TYP_STRT_DT))) || '-' || RTRIM(MONTHNAME(COVG_TYP_STRT_DT)) || '-' ||...
1
by: schaf | last post by:
Hi NG! I have a .NET project which uses a sublibrary written in C++. On the .NET side I use the XMLSerializer to serialize an object (into an MemoryStream). This stream would be casted into a...
2
by: Thelonious Monk | last post by:
I have a problem where some data is being eliminated. The problem is that the data contains signed numeric fields (the low-order byte of a negative number uses the first 4 bits as a sign and...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
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...

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.