How to display a number close to zero in c#? - c#

I have a problem. I have a double number like 0.00000001. Then I should convert it into String and put it in textbox (Convert.ToString(0.00000001)). But that number displayed like 1E-08. Math.Round is not working here. I should display that number like 0.00000 (at least), not just 0.

You can use The numeric ("N") format specifier for that;
(0.00000001).ToString("N5").Dump(); // 0,00000
(0.00000001).ToString("N6").Dump(); // 0,000000
(0.00000001).ToString("N8").Dump(); // 0,00000001
Since my CurrentCulture's NumberDecimalSeparator is ,, it represents it as 0,0 not 0.0. If it is the same for you, you can use InvariantCulture as a second parameter in your .ToString() method.

Related

remove decimal points from percentage and keep percent symbol in .net

I am trying converting decimal to string which are in this format 0.85 to 85% by using below code, but i am always getting like this
85.00 %
and the code i am using like this below
item.ModifiedObject.Diversity.ToString("p", CultureInfo.CurrentCulture);
I am not sure where i am doing wrong, here i need to remove decimal places and at the same time i need to keep percent symbol with this.
Could any one suggest any suggestion on this that would be great full to me.
PS: I do not want to use split() function with respect to value
It's always useful to look at the documentation first, so let's visit it
The Percent ("P") Format Specifier
The percent ("P") format specifier multiplies a number by 100 and
converts it to a string that represents a percentage. The precision
specifier indicates the desired number of decimal places. If the
precision specifier is omitted, the default numeric precision supplied
by the current PercentDecimalDigits property is used.
Eg
decimal d = 1.23M;
Console.WriteLine(d.ToString("P0"));
Console.WriteLine(d.ToString("P1"));
Console.WriteLine(d.ToString("P2"));
or
decimal d = 1.23M;
Console.WriteLine($"{d:P0}");
Console.WriteLine($"{d:P1}");
Console.WriteLine($"{d:P2}");
Output
123%
123.0%
123.00%

ParseExact cannot parse a string in RFC 3339 Internet Date/Time format

It seems that C# does not manage to parse a time in a valid RFC 3339 format:
DateTime.ParseExact("2019-12-31T00:00:00.123456789+01:00", "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffffzzz", null)
This line throws an exception, while this line works just fine:
DateTime.ParseExact("2019-12-31T00:00:00.1234567+01:00", "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzz", null)
So it seems there is a limit on milliseconds, but I cannot find out any documentation on that. Is this how it is supposed to be?
The reason want to parse this date is that I have have an input date field. We use OAS (Swagger) date-time format that quite clearly says that any date in RFC 3339 Internet Date/Time format should be valid. Now from the spec here section 5.6
time-secfrac = "." 1*DIGIT
As far as I understand this means that up to 9 digits should be allowed and to be 100% compliant we have to allow these inputs, but it does not seem that C# even supports that.
Any ideas on how to fix it?
Per MSDN specification, you can use only fffffff
The fffffff custom format specifier represents the seven most
significant digits of the seconds fraction; that is, it represents the
ten millionths of a second in a date and time value.
In your first example
DateTime.ParseExact("2019-12-31T00:00:00.123456789+01:00", "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffffzzz", null)
you are using fffffffff which is more precise for .NET custom date and time format strings
As far as I know, .NET supports seven most significant digits for milliseconds which is The "fffffff" custom format specifier are for.
The "fffffff" custom format specifier represents the seven most
significant digits of the seconds fraction; that is, it represents the
ten millionths of a second in a date and time value.
Although it's possible to display the ten millionths of a second
component of a time value, that value may not be meaningful. The
precision of date and time values depends on the resolution of the
system clock.
That means you are giving not meaningful data that are not supported for .NET Framework. I strongly suggest not doing that.
In addition to the information in the other answers, if you cannot change your input and you still want to parse it, you may use one of the following solutions:
If your input will always be in the same format (i.e., has 9 seconds-fraction digits), you could just remove the two extra ones and proceed to parse it:
string input = "2019-12-31T00:00:00.123456789+01:00";
input = input.Remove(27, 2);
// TODO: parse `input`.
If you don't know the number of the seconds-fraction digits beforehand, you may use something like this:
string input = "2019-12-31T00:00:00.123456789+01:00";
string format = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'FFFFFFFzzz";
var regEx = new Regex(#"^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{1,7})\d*");
input = regEx.Replace(input, "$1");
DateTime parsedDate = DateTime.ParseExact(input, format, null);

How to convert an integer value to a special format

I have a numeric value. I want to format it like this:
input: 500000 $
output: 500.000 $
How do I do that?
You can use custom format strings to handle displaying the number the way you want without having to actually change its value.
As long as that dot character is actually a thousands separator in your culture, you can use the format string ##,# $.
var value = 50000;
Console.WriteLine(value.ToString("##,# $"));
# acts as a numeric place holder,
, acts as the separator (this
particular string can handle larger numbers, the groups are repeated for each "thousand" grouping, see this for more detail on how it works),
$ is displayed
literally.
The output is : 50.000 $ as requested.
Also, because this formatting is culture dependant, it will display correctly if the program happens to be run in a country that uses a different separator.
If 500.000 $ happens to be the "normal" way your culture displays currency, you can use the standard format string C0. This simple formats the number with your culture's default currency format, with 0 decimal places. This will also automatically adjust for the culture of the person running the application.
If you do NOT want these to adjust to the culture format of the user, you can pass an explicit culture to ToString.
var value = 50000;
var culture = new CultureInfo("fr-FR");
Console.WriteLine(value.ToString("C0", culture));
This will display 500 000 € on my machine, even though the default culture (en-US) would cause it to display $50,000
Your question is quite easy to solve and you should be able to do it.
In pseudocode (You are gonna have to write the program):
float num
Writeline input number;
read line num;
num = num / 1000;
print num;

ToString("D3") is not working

double Cost = 0.03;
var ttt = Cost.ToString("D3");
and
System.FormatException: Format specifier was invalid.
Why?
http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#DFormatString
Says it's ok?
Take another look at your MSDN link, just a few sections higher up in the same document:
"D" or "d"
Decimal
Result: Integer digits with optional negative sign.
Supported by: Integral types only.
Precision specifier: Minimum number of digits.
Default precision specifier: Minimum number of digits required.
More information: The Decimal("D") Format Specifier.
1234 ("D") -> 1234
-1234 ("D6") -> -001234
(Emphasis mine)
If you want to ensure three digits to the left of decimal point (this is what 'D' does) with a floating-point type value, you will need to use a Custom Numeric Format String.
Cost.ToString("000.########");
But based on your comments, you really want it to the right of the decimal point, in which case the 'F' strings will work:
Cost.ToString("F3");
And if you're worried about the leading zero, you can do this:
Cost.ToString(".000");
based on your comment (4.4546 should be displayed as a string "4.455"), this should work:
var cost = 4.4546d;
var ttt = cost.ToString("0.000");
Probally 'Cost' is a floating point value. The "D" is a 'decimal' format specifier and doesn't works with floats.
Maybe what you need is in this article: http://msdn.microsoft.com/en-us/library/dd260048(v=vs.110).aspx

Convert time zone into a double

I would like to know how I can convert the timezone you get from
TimeZoneInfo.Local.BaseUtcOffset.TotalHours.ToString() (in this case, the code entered would display a number -7, my timezone). The problem is that if I enclose that line of code with a Convert.ToDouble() method, it gives an error because of the negative symbol. It wouldn't be a problem if the timezone I was in didn't have a negative symbol to it. Is there a way to get just the number and not the negative symbol? (I can figure out negative timzones later...)
Get the absolute value via the Math.Abs method, like this:
int value = (int)Math.Abs(TimeZoneInfo.Local.BaseUtcOffset.TotalHours);
Note: This will make -7 return 7.

Categories

Resources