Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

Formating a floating point number

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Eitan - 25 Jan 2008 15:19 GMT
Hello,

I have a float point variable defined as: "float floatVar" with the value of
"12.3".  I would like to format it using floatVar.ToString() in such a way
that the output will be 1. right aligned and 2. it will have 4 characters
left of the decimal point and 2 right of the decimal point i.e. “  12.30”.

Any suggestion how best to do that?

Thanks
EitanB
Lasse Vågsæther Karlsen - 25 Jan 2008 15:34 GMT
> Hello,
>
[quoted text clipped - 7 lines]
> Thanks
> EitanB

Experiment with something like:

String.Format("{0,-7:0.00}", floatVar)

A bit unsure if that 7 should be positive or negative, but one is
left-aligned, the other is right, and thus padded to be 7 characters.

I think you can use :G2 or a similar syntax as well, to use a format
more in line with your regional settings, just with two decimals.

If the above example doesn't work, dig into the String.Format function
and if you follow the links you'll find something about the format
characters for floating point values.

Signature

Lasse Vågsæther Karlsen
mailto:lasse@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3

Arne Vajhøj - 27 Jan 2008 03:00 GMT
>> I have a float point variable defined as: "float floatVar" with the
>> value of "12.3".  I would like to format it using floatVar.ToString()
[quoted text clipped - 7 lines]
>
> A bit unsure if that 7 should be positive or negative,

positive

> I think you can use :G2 or a similar syntax as well,

:F2

> If the above example doesn't work, dig into the String.Format function
> and if you follow the links you'll find something about the format
> characters for floating point values.

http://blog.stevex.net/index.php/string-formatting-in-csharp/ is
better than the MS docs in my opinion.

Arne
BlackWasp - 26 Jan 2008 12:14 GMT
A really simple way would be to use something like...

floatVar.ToString("0.00").PadLeft(7)

Signature

BlackWasp
www.blackwasp.co.uk

> Hello,
>
[quoted text clipped - 8 lines]
> Thanks
> EitanB
loralosangeles - 11 Apr 2008 07:53 GMT
Signature

loralosangeles

> Hello,
>
[quoted text clipped - 7 lines]
> Thanks
> EitanB
Duy Lam - 13 Apr 2008 12:06 GMT
You can check this link for detail:
http://msdn2.microsoft.com/en-us/library/0c899ak8(VS.85).aspx

floatVar.ToString("0000.00") --> 0012.30

Signature

Thanks,
Duy Lam Phuong


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.