c# Custom String Formatting - c#

We have an import service for stores into our database. The latitude and longitude columns are not currently being formatted on import and have resulted in invalid values being inserted into our db. I therefore need to format any lat/long to have 3 places before the decimal and up to 6 after which I thought would be Format("###.######") but it doesn't seem to work. Input values such as 38.921322 or -12.235 have not seemed to conform to the formatting provided. Could anyone a bit more experienced in the area of C# string formatting shed light on how to achieve this? Thank you in advance.

Have you tried String.Format("{0:000.000000}", value);?
Console.WriteLine("{0:000.000000}", 123);
Outputs: 123.000000

Use "0" instead of "#". From MSDN:
0 Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.
# Replaces the "#" symbol with the corresponding digit if one is present; otherwise, no digit appears in the result string.

Related

I'm trying to capture a string using regex. the following is a sample of the text file im trying to extract the data from

sample data1: value1 sampledata2: value value 2
sample data3: data3 sampledata5:
sampledata4: value value value2
sampledata6: sampledata7: value-value,value
I tried the following regex:
(*keywordsample* *\d+ *:[ ]{0,25})([\w\-\,\.] {0,2})+
I assumed that if there 25 whitespaces after the keyword:, then the value is null for that keyword.
values have 2 spaces at most. Ex:
value value - valid
valuevalue - valid
value value-invalid(3 whitespaces between values)
The following data has values:
sample data1-value1
sampledata2-value value 2
sample data3-data3
sampledata5-null
sampledata4-value value value2
sampledata6-null
sampledata7-value-value,value
However, i think the 25 spaces is not safe.
Is there any other way to implement this?
Will there ever be any other value than whitespace?
You can reverse the logic and check for anything, not whitespace. Use it in conjunction with the end of line specifier and the Multiline option and you should be good to go.
Something like:
keyword\s*([^\s]*)$
Based on comments:
Okay nevermind my previous edit ;) I Finally got behind a computer and nailed it (probably needs some minor tweaks to fit your case exactly):
keyword:\s*([^\s:]+\s{0,2}[^\s:]*)\s[^s]+:
The lesson here is you should look for something that can be used as a delimiter. In this case, there is not much to work with, except the knowledge that there will be another keyword followed by a :
Again, for values at the end of a line use:
keyword:\s*([^\s:]+\s{0,2}[^\s:]*)$
(?<=keyword {0,1}: *)\b(\w+ {0,2})(?!( {0,2}\w)+:)\b
finally discovered the answer thank you for the ideas

Decimal not showing leading zero

I have the following code that formats a column as decimal
It works fine however if the number is less than 1 the zero is not displayed
foreach (var deciCol in decimalIndx)
{
var col = deciCol.Start.Column;
sheet.Column(col).Style.Numberformat.Format = "#.####";
}
Input 12.35486 ==> in excel 12.3548 (OK)
Input 0.34845 ==> in excel .3484 (0 is not displayed)
Input 0 ==> in excel (0.) (how can i remove the decimal separator)?
Thank you in advance
Edit:
Thanks to the answer below, i used the following:
"0.0###"
# means optional digit. Use 0 for a leading zero, eg "0.####".
The format string is the same format string you'd use in Excel if you selected a Custom format code. You can test the format string in Excel first and once you find the one you want, use it in EPPlus.
The contents of a custom numeric format string are documented in Excel's docs. Check Create or delete a custom number format. This explains how to specify different formats for positive, negative, zero amounts, include extra text etc.
It would seem that you can even specify colors in the format string. I wonder how [Blue]0.###;[Red]-0.### would look
UDPATE
As the linked page shows, you can specify a different format for zero, eg :
"0.####;-0.###;0"

How to remove char 8236

I'm trying to parse some phone numbers, and I have a function to check if the parsed string is made up of only numbers or the + sign.
In some of them there is an hiden character of value 8236.
Comparing it against '\0' and '\u8236' doesnt work...
What is this character and how do I remove it?
Thanks to #Maximilian Gerhardt who sent this link in a comment https://www.fileformat.info/info/unicode/char/202c/index.htm
I was able to know that 8236 corresponds to character '\u202c'
So I did str.Trim('\u202c')
And it did work
edit:
The simple way to get the corresponding code is to convert from decimal to hex.
8236(decimal) -> 202C(hexadecimal)
I had the same issue, but with character 8237, which led me to this post.
This corresponds with character \u202d.

String format C#

I want to get the currency value in following format after doing string.format.
I have used several formats but none of them are like what i wanted.
Value After Format
0 $0
123 $123
1234 $1,234
12345 $12,345
I have used following code and it works fine.
Console.WriteLine(string.Format("{00:$#,#}", 12345));
But the problem is that when value becomes 0 then it prints just $ whereas i want to print $0
Thanks
The # in the format string means display a digit if it is not zero. So when you have a 0 value, then it will not display anything.
So you need to change this to
Console.WriteLine(string.Format("{00:$#,0}", mynumber));
But you can also use the inbuilt currency string formatting option.
The docs have a whole section on currency formatting
https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#CFormatString
So then you can have
Console.WriteLine(string.Format("{0:c}", 99.556551));
Or if you don't want any decimals places then
Console.WriteLine(string.Format("{0:C0}", 99.556551));
Although this will round the value as well, so it might be better if you control this yourself.
And as reminded by #RufusL below, the string.format is not needed in a Console.Writeline, so you can also simplify this further;
Console.WriteLine("{0:C0}", 99.556551);

Custom Numeric Format Strings: Dynamic Decimal Point

I am trying to format a double in C# such that it uses the thousand separator, and adds digits upto 4 decimal places.
This is straight forward except that I dont want to have the decimal point if it is an integer. Is there a way to do this using the custom numeric format strings rather than an if statement of tenary operator?
Currently I have:
string output = dbl.ToString(dbl == (int)dbl ? "#,##0" : "#,##0.####");
Thanks
I believe your second format string of "#,##0.##" should be exactly what you want -- the # format character is a placeholder that will NOT display zeros.
If you had "#,###.00" then you would get trailing zeros.
test code:
double d = 45.00;
Console.Writeline(d.ToString("#,##0.##"));
Gives output of "45". Setting d to 45.45 gives output "45.45", which sounds like what you're after.
So you had the answer after all! ;)
Incidentally, there's a handy cheat-sheet for format strings (amongst other handy cheat-sheets) at http://john-sheehan.com/blog/net-cheat-sheets/
No, there is not any built-in format string for this. Your current solution is the best way to accomplish this.
MSDN lists both the standard numeric format strings and custom numeric format strings, so you should be able to see for yourself that none directly matches your needs.

Categories

Resources