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;
Related
while using String.Format("{0:n}", 1234) I am getting the output to three decimal places while I require up to two decimal places with comma
I am getting the ouput as 1,234.000 in QA environment and 1,234.00 in dev environment for the same code.
From the documentation:
The precision specifier indicates the desired number of digits after the decimal point. If the precision specifier is omitted, the number of decimal places is defined by the current NumberFormatInfo.NumberDecimalDigits property.
The result string is affected by the formatting information of the current NumberFormatInfo object. The following table lists the NumberFormatInfo properties that control the formatting of the result string.
Essentially, the thread your code is running in has a culture (System.Threading.Thread.CurrentThread.CurrentCulture) and that defines a property (NumberFormat) and its NumberDecimalDigits property sets the default number of decimal places to be used for this culture. Without specifying a culture to use when formatting the number as a string, the thread culture is used.
If you want to force 2 decimal places, you should change your code to this:
string result = String.Format("{0:n2}", 1234);
Or you could force String.Format to use the invariant culture:
string result = String.Format(CultureInfo.InvariantCulture, "{0:n}", 1234)
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);
I'm trying to get a string formatted float number (e.g. "3.14") from a COM port.
I used Convert.ToSingle() but it threw exception "Input string was not in a correct format" And while debugging, I found that double, float and decimal numbers are separated by '/' instead of '.'; for example 3.14 was 3/14.
My system language is English, but date and time formats are in Persian (Windows 10). In Persian, we use / instead of . as the decimal symbol.(۳/۱۴ = 3.14)
Is there any way to make program independent of system regional settings and force it to always use '.' as decimal symbol?
Using Convert.ToSingle will attempt to convert an object to a floating-point number based on the system's region settings, as you have already noticed.
In order to reliably convert a string that is, for example, in US-English format, you can provide an additional argument of the type IFormatProvider to the method.
string text = "3.5";
IFormatProvider culture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
float number = Convert.ToSingle(text, culture);
The result stored in number be 3.5, i.e. the number halfway between three and four, independent of your system settings. For example, the above code works as expected on my computer, even though it's set to the German (de-DE) region, which represents the same number as 3,5.
See also the documentation of Convert.ToSingle for details.
In my program on the WindowsForms (VS13) I have a label, which contains a value of the variable:
label6.Text = indicators.money.ToString();
When I'm doing like this, I'm getting a value of the variable money on the screen: 50. But i need to display it like 50$. How to do that without adding one more label?
.Net framework provides a way for showing currency sign with monetary values.
For your case you can do:
label6.Text = indicators.money.ToString("C");
But as Jon Skeet has pointed out in comment:
approach here will use the current culture to determine not only the
appropriate currency symbol, but also how/where to display it.
So, if your culture is en-US you will get the currency sign before the value like:
$23.25
You can change that for the culture using properties: NumberFormatInfo.CurrencyPositivePattern Property and NumberFormatInfo.CurrencyNegativePattern Property. Like:
CultureInfo ci = new CultureInfo("en-US");
ci.NumberFormat.CurrencyPositivePattern = 1;
decimal money = 23.25M;
string str = money.ToString("C", ci);
then you will get:
"23.25$"
CurrencyPositivePatter expects an int value which determines the position of currency symbol, For en-US culture it would be like:
0 $n
1 n$
2 $ n //(with a space)
3 n $ //(with a space)
You may see: Standard Numeric Format Strings - Currency Format specifier
The "C" (or currency) format specifier converts a number to a string
that represents a currency amount. The precision specifier indicates
the desired number of decimal places in the result string. If the
precision specifier is omitted, the default precision is defined by
the NumberFormatInfo.CurrencyDecimalDigits property.
I have written a small program where the program works differently on different operating systems (xp, win7) The problem is the program reads some float numbers such 2,686.
One operating system (win7) convert it to float true, but on xp it goes wrong and print it 2686. How can I understand which symbol the operation system uses for decimal numbers ?
Thanks.
string sep = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
This does not depend on the operating system but at the (default) language settings on each PC.
If you use : double value = double.Parse(text); you are using whatever culture the user has configured. If you know the input to be in a certain format, use:
var ci = CultureInfo.GetCulture("nl-NL"); // dutch
double value = double.Parse(text, ci);
Every function that converts has (1 or more) overloads to take a FormattingProvider (Culture).
parse the floating point numbers using the user current culture with double.Parse(string, System.Globalization.CultureInfo.CurrentCulture);
The decimal separator is decided by the current culture.
If you want to use a specific character as decimal separator, you can create a custom NumberFormatInfo object with any separator you like. If you want to use period as deimcal separator, you can simply use InvariantCulture:
double n = Double.Parse(s, CultureInfo.InvariantCulture);
If you want to use comma, you can choose a culture that has that, for example swedish:
double n = Double.Parse(s, CultureInfo.GetCultureInfo("sv-SE"));