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

One significant figure in C#

I feel a bit ashamed asking this after 6 years of C#, but is there a
format string for significant figures before the decimal place?

e.g.
2,354,856:
1 sf = 2,000,000
2 sf = 2,400,000
3 sf = 2,350,000

{0:G} or g gives me scientific notation, but I'm after the above.

Thanks
Jun 27 '08 #1
2 2335
<mr*********@googlemail.comwrote in message
news:9b**********************************@a1g2000h sb.googlegroups.com...
>I feel a bit ashamed asking this after 6 years of C#, but is there a
format string for significant figures before the decimal place?

e.g.
2,354,856:
1 sf = 2,000,000
2 sf = 2,400,000
3 sf = 2,350,000

{0:G} or g gives me scientific notation, but I'm after the above.
The following doesn't fully answer your question but might help, it's a
customer formatter I've cobbled together based on the code at:

http://msdn.microsoft.com/en-us/libr...tprovider.aspx.

I believe the rounding need to be implemented in the
SFFormatProvider.Format() method to achieve the exact results you're looking
for but this shouldn't be too difficult.

class Program
{
static void Main(string[] args)
{
int val = 2354856;
Console.WriteLine(String.Format(
new SFFormatProvider(), "{0}", val));
Console.WriteLine(String.Format(
new SFFormatProvider(), "{0:1}", val));
Console.WriteLine(String.Format(
new SFFormatProvider(), "{0:2}", val));
Console.WriteLine(String.Format(
new SFFormatProvider(), "{0:3}", val));
}
}

public class SFFormatProvider : IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
else
return null;
}

public string Format(string format, object arg,
IFormatProvider formatProvider)
{
char[] carr = arg.ToString().ToCharArray();
int digits = 1;
if (!String.IsNullOrEmpty(format))
digits = Int32.Parse(format);

for (int i = digits; i < carr.Length; i++)
{
carr[i] = '0';
}
return String.Format("{0:n0}",
Convert.ToInt32(new String(carr)));
}
}

Regards
David
Jun 27 '08 #2
mr*********@googlemail.com wrote:
I feel a bit ashamed asking this after 6 years of C#, but is there a
format string for significant figures before the decimal place?

e.g.
2,354,856:
1 sf = 2,000,000
2 sf = 2,400,000
3 sf = 2,350,000

{0:G} or g gives me scientific notation, but I'm after the above.
I don't think so.

You will need to code it yourself.

One way:

public static string SpecialFormat(int v, int sf)
{
int k = (int)Math.Pow(10, (int)(Math.Log10(v) + 1 - sf));
int v2 = ((v + k/2) / k) * k;
return v2.ToString("0,0");
}

(the calculation of k can be optimized if speed is critical)

Arne

Jun 27 '08 #3

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

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
29
by: mrstephengross | last post by:
Hi all... How can I find out the number of significant digits (to the right of the decimal place, that is) in a double? At least, I *think* that's what I'm asking for. For instance: 0.103 --> 3...
8
by: David Corby | last post by:
Hi everybody, I've got a problem. I'm trying to round a double to a particular number of significant digits, in this case 5, but I can't figure out a way around getting _exactly_ what I want...
3
by: mrstephengross | last post by:
Hi all... How can I find out the number of significant digits (to the right of the decimal place, that is) in a double? At least, I *think* that's what I'm asking for. For instance: 0.103 -->...
1
by: Phil | last post by:
Recently, using PERFMON, I've been rather dismayed to find that our application is averaging 3 - 4 lock timeouts per second, and frequently has extended periods of several minutes where this...
56
by: infidel | last post by:
Where are they-who-hate-us-for-our-whitespace? Are "they" really that stupid/petty? Are "they" really out there at all? "They" almost sound like a mythical caste of tasteless heathens that "we"...
7
by: Michael Howes | last post by:
MSDN documentation for format strings seems ambiguous. It's says things like "significant digits or number of decimal places" all over the place. Well those two things are different. How do I...
2
by: Mukesh_Singh_Nick | last post by:
What is meant by the "most significant digits" in the following statement? <BLOCKQUOTE> With %g and %G, the precision modifier determines the maximum number of significant digits...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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: 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...

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.