Format an integer like a timezone offset (ex. +02:00) - c#

I have a number between -12 and 12. How can I get it in this format: "+number:00"
Ex. for -4: -04:00
Ex. for 10: +10:00

That's more precise
number.ToString("+00;-00") + ":00"
-OR-
number.ToString("+00':00';-00':00'")
-4 --> -04:00
10 --> +10:00

If you just concerned about formatting the number to particular format with signs (positive/negative), you could
var positive = 5;
var negative = -12;
var strPositive = positive.ToString("+00':00';-0#':00'");
var strNegative = negative.ToString("+00':00';-0#':00'");
Output
+05:00
-12:00
You are making use of Conditional Formatting here. You can read more on the same here
Quoting.
The semicolon (;) is a conditional format specifier that applies
different formatting to a number depending on whether its value is
positive, negative, or zero
For a format string with two sections
The first section applies to positive values and zeros, and the second
section applies to negative values.
If the number to be formatted is negative, but becomes zero after
rounding according to the format in the second section, the resulting
zero is formatted according to the first section.

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%

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;

How to display a number close to zero in 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.

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

Creating log with millisecond portion in time

I am writing activities of a program in a log with timestamp attached to each line with the following format:
DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss FFF")
I was expecting the millisecond portion to always have 3 digits. However the following appears:
11/29/2013 18:53:14 96 - SessionKey-2rvun1agyw1svcexmqf5dejh-MAC.Master Page_Load - Begin
11/29/2013 18:53:14 975 - SessionKey-2rvun1agyw1svcexmqf5dejh-MAC.Master Page_Load - End
In the first one, why is the millisecond portion not 096?
From The "FFF" Custom Format Specifier
The "FFF" custom format specifier represents the three most
significant digits of the seconds fraction; that is, it represents the
milliseconds in a date and time value. However, trailing zeros or
three zero digits are not displayed.
EDIT: Ok, I think I figure out what's going on here. In original, OP's millisecond part is 960 not 096.
That's why it is working exactly how describes in MSDN page.
It's trailing 0 at the end of 960, it is not leading 0 in 096.
As a solution, you can use The "fff" Custom Format Specifier instead. It doesn't trail or leading zeros in my opinion. At least it doesn't write in MSDN page :)
EDIT2: I think people are confusing what is trailing zero and leading zero
From Wikipedia pages;
Trailing zero
In mathematics, trailing zeros are a sequence of 0s in
the decimal representation (or more generally, in any positional
representation) of a number, after which no other digits follow.
Leading zero
A leading zero is any 0 digit that leads a number string
in positional notation. For example, James Bond's famous identifier,
007, has two leading zeros.
Use:
"MM/dd/yyyy hh:mm:ss fff"
on my machine:
hh:mm:ss.fff leading zeros
hh:mm:ss.FFF NO leading zeros
sample code:
for (int i = 0; i < 2222; i++)
{
Console.WriteLine(DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss fff"));
}
string input;
input = Console.ReadLine();

Categories

Resources