I have a string
var str = "2021-11-19T19:45:30.810Z" but if I use
DateTime.TryParse(str,out datefr)
And pass this datefr value to Api, in log file seeing only
"2021-11-19T19:45:30.81+05:30"
where the millisecond is displaying only as 81 instead of 810.
Is there any way to pass this date along with three digit millisecond(with the trailing zero) to datetime variable
Tried DateTime.ParseExact() but still the trailing zero are not passing
Related
I want to remove the characters after 2 decimal values. while I'm formatting not getting any changes in the value. I don't want convert it to double and then string due to performance issue may arise.
data type is Float in the database
value -172.209
Exp op - 172.20
code
string max_demand = dt_max_demand.Rows[0][0].ToString();
max_demand= String.Format("{0:0.0#}", max_demand);
You could search for the last '.' character in your string and then cut it after 2 more chars but it would be way more efficient and easier to format it using $"{dt_max_demand.Rows[0][0]:F2}", assuming dt_max_demand.Rows[0][0] has a numeric type before you cast it to a string.
string max_demand = $"{dt_max_demand.Rows[0][0]:F2}";
I have this tag in a XML
<seguroTotalReais>000000000037103</seguroTotalReais>
I need to set this value like a decimal with 2 places for example 371,03.
Is there a way to do this without manipuling string directly?
You have a couple of options, personally I would not do string operations on this. If you are looking to shift the decimal 2 places i would convert and then divide by 100. This will also give you some validation on whether its a numeric value or not.
string dec = "000000000037103";
// I did a string replace for swapping the dot to a comma.
Console.WriteLine((Convert.ToDouble(dec)/100).ToString("0.##").Replace(".",","));
You can also utilize Culture info to affect how your numbers are displayed as some culture info's utilize the comma as the decimal separator.
I am getting some percent values from a database and I need to format them to have the correct thousands seperator, number of decimal places and a percent sign on the end.
I tried this:
string text = "105,3"; //example, formatting like database input
string format = "#,##0.##";
e.Row.Cells[i].Text = double.Parse(text).ToString(format);
Weirdly this returns 1053,00%. How do I make it so it returns 105,30%? (The decimal comma is because the system locale is german, so it's how it is supposed to be)
edit: replacing the comma with a period results in 10530.00%. Nothing makes sense to me anymore.
edit2: the float.Parse() actually works just fine. the ToString() messes everything up. I played around with using different cultural settings and format strings (switching comma and period) but it only makes it worse again.
Pass the current Culture to the Parse method: double.Parse( text, CultureInfo.CurrentCulture )
However, this only works on systems that use a locale that has the comma as a decimal separator.
If you want this to work on other locales you should replace CurrentCulture with the specific CultureInfo instance that used when inputting data in the first place.
The title is misleading. The actual problem was the ToString() function. In the format string I added the % sign, which, to be fair, I didn't add in the original post because I forgot about it. It automatically multiplies the number by 100. So my format string is now "#,##0.00\%".
I'm implementing string format number with three sections as
DataFormatString="{}{0:$#,##0.00;($#,##0.00);''}"
i.e:
input 120 result $120.00
input -120 result ($120.00)
input 0 result ''
the problem is the dollar sign is hard-coded and I want my app to globalization. I tried some thing like this:
DataFormatString="{}{0:C;C;''}"
but it doesn't help.
Perhaps try taking this approach:
var DataFormatString="$#,##0.00;($#,##0.00);''";
var amount = 1342.56m;
var formatted =
amount
.ToString(DataFormatString)
.Replace("$", CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol);
The only downside is that it won't correctly place the currency symbol at the end of the number if that's the standard for that currency.
You can use the overload of string.Format with IFormatProvider like so:
string DataFormatString="{0:C}";
string output = string.Format(CultureInfo.CreateSpecificCulture("culture"),DataFormatString,input)
Demo
However, as Soner Gonul observes, this still won't generate '' as output for 0.
As far as I know, there is no direct format to supply that 3 results you want for those inputs.
For first two format, you can use The "C" format specifier with en-US culture (which has $ as a CurrencySymbol) using 2 as a precision specifier like;
(120).ToString("C2", CultureInfo.GetCultureInfo("en-US")) // $120.00
(-120).ToString("C2", CultureInfo.GetCultureInfo("en-US")) // ($120.00)
But for 0, this generates $0.00 which is not what you want.
As a better solution, check Enigmativity's answer instead which handles 0 value as well.
string listOfItemPrices = items.ToSemiColonList(item => string.Format("{0:C}", item.Price.ToString()));
I am simply trying to format the price here to 2 decimal places. Ok, so the string.Format doesn't implement IFormattable? Ok not sure how to get around this so that I can format the decimal (price) here.
By passing item.Price.ToString() to String.Format, you are passing a string, not a decimal.
Since strings cannot be used with format strings, you're getting an error.
You need to pass the Decimal value to String.Format by removing .ToString().
There is no point using string.format here, that is used for adding formatted values into strings. e.g.
String.Format("This is my first formatted string {O:C} and this is my second {0:C}",ADecimal,AnotherDecimal)
If you just want the value of a decimal variable as a formatted string then just pass the string formatter to the ToString() method e.g.
ADecimal.ToString("C");