I have the following in a razor view:
<td>#payments.Sum(p => p.Amount)</td>
I want it to display as a currency so have '$' and two decimals on the end.
I think normally you go something like {0:C}.
I don't know how to incorporate this into what I've got as the Sum does not have an overload for format.
Do I need to do this with a css class?
You would do this:
<td>#String.Format("{0:C}", payments.Sum(p => p.Amount))</td>
<td>#payments.Sum(p => p.Amount).ToString("C")</td>
I think this is a slightly more efficient call than String.Format. See this answer.
Related
I need to pass multiple values as one param in the query string. Is there a better way to do it than the below example? I do not want to use session to send these values from page to page.
For example: http://www.mywebapp.com/Products.aspx?CategoryIds=45|29|98
Is pipes the best way to do something like this? I noticed they get turned into % symbols but it does not mess up my code when I do:
Request["CategoryIds"];
I had the similar situation and I am using comma to separate the numbers in query string.
Even in your case, looks like the CategoryIds are all numbers. So, I think comma should work for you.
And the URL will look something like this: http://localhost:62100/Contact.aspx?CategoryIds=1,2,3
Buggy control provides in text something like this:
{{\field{\*\fldinst{HYPERLINK http://yandex.ru }}{\fldrslt{http://yandex.ru\ul0\cf0}}}}\f0\fs24
but correct version is:
{{\field{\*\fldinst{HYPERLINK http://yandex.ru }}{\fldrslt{\ul\cf1 http://yandex.ru}}}}\f0\fs24
I'm really newbie in regex and other text tools, so I don't know how replace all occurrences with correct variant in righteous way. We can't rewrite control logic now, there is more WinAPI code.
Platform is .NET Framework 2.0
Well, basically regular expression you've generated is ok, as it does in job and find all occurences like {http://yandex.com\ul0\cf0}.
If I have understood your goal correctly - the only transformation you need in each capture group - is transform {http://yandex.com\ul0\cf0} to {\ul\cf1 http://yandex.com}.
This can be done easily with Regex.Replace override having MatchEvaluator as argument.
For example, something like this (note, it is not most elegant solution, rather it is "quick and dirty"):
var result = Regex.Replace(source_Text, regex_pattern,
x => x.Groups[0].Value.Replace(#"\ul0\cf0", "").Replace("{", #"{\ul\cf1 "));
Sorry if this has been answered - I have read at least 20 questions that answer an almost identical question but they're all with slightly different circumstances or I can't get them to work.
That's also despite the fact what I want is far more simple! :(
All I want to do is return all the elements of a string array that start with "ABC".
So {"ABC_1", "ABC_2", "ABC_3", "123_1", "ABC_4"}
Will return {"ABC_1", "ABC_2", "ABC_3", "ABC_4"}
Using Linq with Lambda Expressions:
So the logic would be similar to the following:
FilteredArray = StringArray.Where(String.StartsWith("ABC"));
Everytime I try something similar to the above it returns no values.
Thank you in advanced for your help and apologies in advanced for not being able to solve this independently.
P.S If you know of a link that would provide me with a good tutorial on Linq with Lambda Expressions that would be greatly appreciated.
If you want to make an array from an array, you need to use ToArray. You also need to get your LINQ syntax right:
// This assumes that FilteredArray is declared as String[]
FilteredArray = StringArray.Where(str => str.StartsWith("ABC")).ToArray();
Note that Where is not all-caps because identifiers in C# are case-sensitive. Also note the use of the lambda syntax inside the Where method.
I'm not actually sure the original example would compile? Your missing the lambda syntax. This will work correctly. Basically you need to specify the input parameter for the where clause (each of the strings in the array) this is x. Then you do you're string startswith check on that.
var filtered = StringArray.Where(x => x.StartsWith("ABC"));
You need to use the correct syntax in your lambda expression like this
var FilteredArray = StringArray.Where(x => x.StartsWith("ABC")).ToArray();
If you want to learn about Linq expressions then here is a very good solution available for you 101 Linq Samples
Say I have the following string
[id={somecomplexuniquestring}test1],
[id={somecomplexuniquestring}test2],[id={somecomplexuniquestring}test3],
[id={somecomplexuniquestring}test4],[id={somecomplexuniquestring}test5],
[id={somecomplexuniquestring}test6],[id={somecomplexuniquestring}test7],
[id={somecomplexuniquestring}test8],[id={somecomplexuniquestring}test9]
is there a way just using regex to get the following result [id={somecomplexuniquestring}test6]
{somecomplexuniquestring} are unknown strings which cannot be used in the regex.
For example, the following will not work #"[id=[\s\S]+?test6]" as it starts from the very first id.
Is using RegEx the best solution? You have tagged C#, so would
variableWithString.Split(",").Any(x => x.Contains("test6"));
give you the exists match, or
result = variableWithString.Split(",").Where(x => x.Contains("test6"));
give you the match value you are seeking?
This doesn't work??
\[id={.*?}test6\]
This all depends on exactly what the limitations of somecomplexuniquestring are. For example, if you have a guarantee that they do not contain any [ or ] characters, you can use this simple one:
"\[[^\[\]]*test6\]"
Similarly, if it could contain square brackets but no curly braces, you can do something similar:
"\[id={[^{}]*}test6\]"
HOWEVER, if you have no such guarantee, and there's some sort of escaping system for including {} or [] in that string, then you need to let us know how that works to properly answer.
You can use this pattern:
#"\[[^]]*]"
If you want a specific test number you can do this:
#"\[id={[^}]*}test6]"
I am formatting the currency using Tostring() method i m using following syntax
ToString('##.##') it is working perfectly but in case of round number it remove last 2 zero
like for 100 it does not show 100.00 is shows 100.
how can i format in that way means
input desired output
100 100.00
100.10 100.10
Try "##.00" instead.
That will force two digits after the decimal separator.
You can also use ToString("C") to use the culture specific format in Windows directly.
First google result.
String.Format("{0:C}", x.ToString());
http://www.howtogeek.com/howto/programming/format-a-string-as-currency-in-c/
You can use :
.ToString("C")
Hope it helps.
Also, if you don't want the currency sign ($ in the US) added that "C" gives, you can also use "F2", which is "fixed number with 2 decimal places". It also has the advantage of giving you a thousands separator when you results go over 1,000.00.
This might help. Might be more than you need, but it takes globalization into account which might be necessary. "C" is also a short-cut currency format string that might get you further along.