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

How do you Format Floats in C#?

xarzu
88 64KB
In C# code I have a floating point variable number that I want presented as a string but I want it formatted in a particular way. if I have a float number, like, 0.532492829839, for example and I want it represented in C# code as "53.24%", how would I do that? I imagine it would be done using IFormatProvidre and float.parse. But I have not done it before. First of all, I guess I should multiply the number by 10, but how would I pick a number of decimal place?
Aug 4 '11 #1
1 6696
GaryTexmo
1,501 Expert 1GB
There are lots of resources for how the formatting codes work, check out this page: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx (Google for more).

That page suggests you can use the formatting string "P" with the number of decimal places you want, so to get what you want you would write...

Expand|Select|Wrap|Line Numbers
  1. float n = 0.532492829839f;
  2. Console.WriteLine(n.ToString("P2"));
It also seems that the following works...

Expand|Select|Wrap|Line Numbers
  1. Console.WriteLine(n.ToString("0.00%"));
You mark a digit place with a 0 (places a zero or a digit, whatever is available) or # (places a digit or nothing). The % is just text, anything that's not a code just appears as text.
Aug 4 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: John Lenton | last post by:
Is there any reason python's printf-style formatting is missing the (C99) '%a' specifier? I'm sorry if this has been asked and answered before; I can't find it on google ('%a' is a very awkward...
12
by: BGP | last post by:
I am working on a WIN32 API app using devc++4992 that will accept Dow Jones/NASDAQ/etc. stock prices as input, parse them, and do things with it. The user can just cut and paste back prices into a...
2
by: geskerrett | last post by:
In the '80's, Microsoft had a proprietary binary structure to handle floating point numbers, In a previous thread, Bengt Richter posted some example code in how to convert these to python floats;...
10
by: ben | last post by:
i'm learning about the floating point format that's used on my computer (apple mac so powerpc) i've written a function to print out the bits in a float to see how floats are represented and i...
6
by: sbalko | last post by:
Hi, I am trying to read Java-floats (IEEE 754 encoding) stored in a binary file from C (gcc on linux/i386, more specifically). Unfortunately, C seems to expect floats to be stored somewhat...
2
by: Matt McGonigle | last post by:
Hi all, Please help me out with this. Perhaps it is a dumb question, but I can't seem to make it work. I am doing a file conversion using an unformatted binary file for input and outputting to...
6
by: godavemon | last post by:
I need to take floats and dump out their 4 byte hex representation. This is easy with ints with the built in hex function or even better for my purpose def hex( number, size ): s =...
8
by: Yu SONG | last post by:
Hi all, What would be the most efficient way to save an array of floats to a file (in text format)? At the moment, my code looks like: /* * Saving an array of floats to a file
23
by: Arnaud Delobelle | last post by:
Hi all, I want to know the precision (number of significant digits) of a float in a platform-independent manner. I have scoured through the docs but I can't find anything about it! At the...
3
by: ulvr | last post by:
Just a quick question -- are floats on 64 bit machines (Windows/Linux) still going to be 4 bytes, or will they be 8 bytes? Is there any guarentee that a float is four bytes? John
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
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
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?
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,...

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.