How can I display a negative symbol in .NET? - c#

I want to display a negative symbol from a string in .NET. I want a string that represents an equation that looks something like this:
7--5=12
But when displayed, I want the 2nd minus sign to be slightly raised so it looks more natural as a negative sign instead of just 2 minus signs in a row.
Is this possible?

Not sure if theres a character for what you want but a simple solution (and one that would be easily understood and implemented) would be to surround your negative number in brackets:
7 - (-5) = 13

Use the Unicode character SUPERSCRIPT MINUS (U+207B) ⁻.
For example:
7-⁻5 = 13
EDIT: Or, with a MINUS SIGN (U+2212) ⁻ for the minus:
7 − ⁻5 = 13

Provided that you're using unicode you can use a true minus sign, "−" (U+2212) rather than a hyphen-minus, "-" (U+002D). Just be aware that it's not ASCII compatible
Here's your example showing them:
7 - −5=13
Also, here's some fun wiki-articles on all sorts of dash-hyphen-minus lines:
http://en.wikipedia.org/wiki/Dash#Common_dashes
http://en.wikipedia.org/wiki/Minus_sign#Character_codes

This is a great resource on format strings in C#:
SteveX Compiled - Format Strings
You can choose how a negative number is displayed by using a range expression for your format string. It's in the format:
{0:<PositiveFormat>;<NegativeFormat>;<ZeroFormat>}
For example, this is how to display a negative number in parenthesis and the word "Zero" for 0:
{0:#;(#);Zero}
Using this technique, I think you can try it with the superscript version of negative (which is ascii code U+207B) in the negative format string.
{0:#;⁻#;Zero}
HTH, Anderson

Traditionally in math typography you use an en dash U+2013 or minus U+2212 (but not a hyphen!) for both binary (subtraction) and unary (negation) minus, and they are differentiated with spacing (spaces before and after a binary minus, no space between a unary minus and the number it negates).
But if you want to further distinguish the unary, I'd recommend substituting the superscript minus U+207B (but keep the spacing around the subtraction minus):
7 − ⁻5 = 13

You can use the Unicode character U+2212 (Minus Sign): 7-−5=13
In the font I'm using, the minus sign is displayed slightly raised relative to the dash. Your results may vary.

Unicode "superscript minus" http://www.fileformat.info/info/unicode/char/207b/index.htm
char c = '\u207b';

Related

Suggestions needed to Apply SuperScript to C# string For Xsl transformation

I Want to apply SuperScript to String for display
It works fine with numbers in superscript, doesn't work for String characters.
Suggestions needed.
Works fine for :
var o2 = "O₂"; // or "O\x2082"
var unit2 = "unit²"; // or "unit\xB2"
Does not work for :
var xyz = "ABC365\xBTM"
Can not get TM superscripted over string ABC365.
Suggestions appreciated.
You seem to have completely misunderstood what is going on here, so I'll try a very basic explanation.
Unicode defines a large number of characters (some 1,114,111 if I remember right). These came from a large number of historic sources, and there's no great rhyme or reason about which characters made it in and which didn't. The available characters include some subscript and superscript digits, for example x2082 is subscript 2, and x00B2 is superscript 2. It also includes some special symbols such as the trademark symbol x2122 which are traditionally rendered with a superscript appearance.
But there's no general mechanism in Unicode to render any character in superscript or subscript rendition. If you want to write Xn, Unicode won't help you: to achieve that I had to resort to mechanisms outside Unicode, specifically HTML tagging. HTML allows you to render anything as subscript or superscript; Unicode only handles a few select cases.
C# recognizes the escape sequences \xHH and \xHHHH (depending on context), where H is any hex digit, to represent special characters by their Unicode code point value. So if there's a codepoint x2082 meaning subscript 2, you can write it as \x2082 in a Unicode string literal. But there's no codepoint for subscript-lowercase-italic N, so there's no way of representing that.
Now when you write \xBTM it should be clear that's nonsense. \x must be followed by 2 or 4 hex digits (depending on context). If you want the trademark symbol, you can use \x2122. If you want the two characters "T" and "M" in superscript rendition, you're out of luck; if you need to pass that sort of thing around in your application, you will need to pass strings containing HTML markup, rather than just plain Unicode.
You indicate that you're trying to create strings that will be used as input to an XSLT transformation. My suggestion would to pass XML documents rather than plain strings: but I would need to understand the requirement in better detail before saying that's definitively the right solution.

Ignore date in a string with numbers using regular expression

I have a little Problem.
i use [0-9\,.]*
to finde a decimal in a string.
And ([^\s]+) to find the text behind the first number.
The string looks normally like this. 1 number a text and than a date:
1.023,45 stück
24.05.10
but sometimes I had just the date and then i become 240510 as decimal.
And sometimes I had just the decimal.
How should I modify the regex to find the date if existing and remove it?
And then look for a decimal an select this if existing.
Thanks in advance.
Divide and conquer
Check for the date first and remove the match from the string
([0-9]{1,2}\.){2}[0-9]{1,2}
Find the number using your original regex
[0-9\,.]*
If you need it find the unit of quantity (assuming that you will only have it as lower case with u Umlaut)
([a-zü]+)
See http://regexe.de/ (German) and http://www.regexr.com/ (English) for some useful information and tools for dealing with regex.
I suggest matching the number in a more restricted way (1-3 digits, then . + 3 digits groups if any, and a decimal separator with digits, optional).
(?s)(?<number>\d{1,3}(?:\.\d{3})*(?:,\d+)?)\s+(.*?)(?:$|\n|(?<date>\d{2}\.?`\d{2}\.?(?:\d{4}|\d{2})))
See demo
The number will be held in ${number}, and the date in ${date}. If the string starts with something very similar to a date (6 or 8 digits with optional periods), it won't be captured. If the date format is known (say, the periods are always present), remove the ?s from \.?s.
(?s) at the beginning will force the period . to match a new line (maybe it is not necessary).

Regex for check the input string is just in persian language

I work with MVC and I am new on it. I want to check input values is only in Persian language (Characters) by [RegularExpression] Validation.
So I think to use Regex and need to check in range of unicodes, but I don't lnow how can find range of Persian characters Unicode. Am I right about this Regex? what is your suggestion and how can I find range of Unicode in Persian
Persian characters are within the range: [\u0600-\u06FF]
Try:
Regex.IsMatch(value, #"^[\u0600-\u06FF]+$")
Check first letter and last letter range in Persian I think something like this:
"^[آ-ی]$"
Regex.IsMatch(Text, #"^([\u0600-\u06FF]+\s?)+$")
This Only Contain standard Arabic symbols range But Persian also include 4 More Characters:
ژ \uFB8A
پ \u067E
چ \u0686
گ \u06AF
So You Should Use:
^[\u0600-\u06FF\uFB8A\u067E\u0686\u06AF]+$
If you want to match Zero-width-non-joiner you should add this too:
\u200C
TL;DR
All answers that say use \u0600-\u06FF or [آ-ی] are simply WRONG.
i.e. \u0600-\u06FF contains 209 more characters than you need! and it includes numbers too!
Farsi MUST used character sets are as following:
Use ^[آابپتثجچحخدذرزژسشصضطظعغفقکگلمنوهی]+$ for letters.
Use ^[۰۱۲۳۴۵۶۷۸۹]+$ for numbers.
Use [ ‬ٌ ‬ًّ ‬َ ‬ِ ‬ُ ‬ْ ‬] for vowels.
Or a union of those. You may want to add other Arabic letters like Hamza ء to your character set additionally.
This answer exists to fix a common misconception. Codepoints 0600 through 06FF do not denote Persian / Farsi alphabet (neither does [آ-ی]):
[\u0600-\u0605 ؐ-ؚ\u061Cـ ۖ-\u06DD ۟-ۤ ۧ ۨ ۪-ۭ ً-ٕ ٟ ٖ-ٞ ٰ ، ؍ ٫ ٬ ؛ ؞ ؟ ۔ ٭ ٪ ؉ ؊ ؈ ؎ ؏
۞ ۩ ؆ ؇ ؋ ٠۰ ١۱ ٢۲ ٣۳ ٤۴ ٥۵ ٦۶ ٧۷ ٨۸ ٩۹ ءٴ۽ آ أ ٲ ٱ ؤ إ ٳ ئ ا ٵ ٮ ب ٻ پ ڀ
ة-ث ٹ ٺ ټ ٽ ٿ ج ڃ ڄ چ ڿ ڇ ح خ ځ ڂ څ د ذ ڈ-ڐ ۮ ر ز ڑ-ڙ ۯ س ش ښ-ڜ ۺ ص ض ڝ ڞ
ۻ ط ظ ڟ ع غ ڠ ۼ ف ڡ-ڦ ٯ ق ڧ ڨ ك ک-ڴ ػ ؼ ل ڵ-ڸ م۾ ن ں-ڽ ڹ ه ھ ہ-ۃ ۿ ەۀ وۥ ٶ
ۄ-ۇ ٷ ۈ-ۋ ۏ ى يۦ ٸ ی-ێ ې ۑ ؽ-ؿ ؠ ے ۓ \u061D]
255 characters are fallen in this range, Farsi alphabet has 32 letters that in addition to Farsi demonstration of digits it would be 42. If we add vowels (Arabic vowels originally, that rarely used in Farsi) and Tanvin (ً, ٍِ ‬, ٌ ‬) and Tashdid (ّ ‬) that are both a subset of Arabic diacritics not Farsi, we'd end with 46 characters. This means:
\u0600-\u06FF contains 209 more characters than you need!
۷ with codepoint 06F7 is a Farsi representation of number 7 and ٧ with codepoint 0667 is Arabic representation of the same number. ۶ is Farsi representation of number 6 and ٦ is Arabic representation of the same number. And all reside in 0600 through 06FF codepoints.
The shapes of the Persian digits four (۴), five (۵), and six (۶) are
different from the shapes used in Arabic and the other numbers have
different codepoints.
You can see different number of other characters that doesn't exist in Farsi / Persian too and nobody is willing to have them while validating a first name or surname.
[آ-ی] includes 117 characters too which is much more than what someone needs for validation. You can see them all using Unicode CLDR.
I use this RegExp in my program, and it works correctly. hope to help you:
[پچجحخهعغفقثصضشسیبلاتنمکگوئدذرزطظژؤآإأءًٌٍَُِّ\s]+$
Persian characters are within the range: [\u0600-\u06FF] + [\s]
Try:
Regex.IsMatch(Text, #"^([\u0600-\u06FF]+\s?)+$")
This Patern Contains Letter and space Charachters.

icd9 regex pattern

I cannot find a definitive guide to icd9 code formats.
Can anyone explain the format - especially the use of trailing and leading zeros?
A C# regex for icd9 and icd10 codes would also be nice.
Thanks!
I was looking for the same thing and found what I believe to be a more complete answer. Thought I'd help anyone else coming in the future.
ICD-9 Regex
The ICD 9 format has a bunch of ways it can be formatted. It can begin with V, E, or a number.
If it begins with V, then it has 2 numbers, a decimal, then up to
two numbers
Examples: V10.12 and V12
If it begins when E, then it has 3 numbers, the decimal place, then up to two numbers
Examples: E000.0 and E002
If it begins with a number, then it is up to 3 numbers, a decimal, then up to two numbers
Examples: 730.12 and 730
A good regex that checks all these rules is (Credit goes to sascomunitt)
^(V\d{2}(\.\d{1,2})?|\d{3}(\.\d{1,2})?|E\d{3}(\.\d)?)$
ICD-10 Regex
According to www.cms.gov ICD-10 has the following rules:
3-7 Characters
Character 1 is alpha (cannot be U)
Character 2 is numeric
Characters 3-7 are alphanumeric
After 3 characters you use a decimal
Use of dummy placeholder "x" (This is the only one I am not accounting for in my regex...)
Alpha characters are not case sensitive
Here is the regex I came up with:
^[A-TV-Z][0-9][A-Z0-9](\.[A-Z0-9]{1,4})?$
Note These regexes are for javascript and may need tweaked for C# (I'm too lazy to test it right now)
An ICD-9 code looks like this:
two/three-digit numeric code (may have leading zeroes to pad to three digits)
an optional dot
if that dot is present, there will be one or two following digits, depending on the preceding three digits. Which digits are allowed specifically is very variable.
Some codes are prefixed by an E or V.
An ICD-10 code looks like this:
an uppercase ASCII letter (A-Z)
two digits
an optional dot
if that dot is present, there will be one or two following digits. Again, it's highly variable which ICD codes allow for which digits after the dot.
Sometimes, you'll find an asterisk, a plus sign (at least in ASCII texts), or an exclamation point after a code. They are used in certain combination codes.
So, in essence, you could use regex to find ICD codes in a text, but you won't be able to validate them.
A C# regex for ICD-9 codes could look like this: #"\b[EV]?\d{2,3}(?:\.\d{1,2})?\b".
For an ICD-10 code: #"\b[A-Z]\d{2}(?:\.\d{1,2})?\b[*+!]?"
Are you referring to ICD-9 diagnosis codes? Then see this thread: ICD-9 Code List in XML, CSV, or Database format.
There are 2 types of ICD 9 codes: Diagnosis Codes & Procedure Codes
for the Diagnosis Codes Gordon Tucker has the correct answer:
^(V\d{2}(\.\d{1,2})?|\d{3}(\.\d{1,2})?|E\d{3}(\.\d)?)$
ICD-9-CM procedure codes are 2 numbers, a decimal, then up to two numbers (to be a complete code 2 numbers are required)
A regex for these codes would be:
^\d{2}.\d{1,2}$
ICD-9-CM Procedure Codes
2012 ICD-9-CM Diagnosis Codes

Is there a way to specify multiple formats in C# for a decimal string like in VB?

I am translating some code from VB to C# and came across this:
Format(seg.R, " 00.00000;-00.00000") // <-- There is a leading space in the first format string
...which prints...
00.00000 //or...
-00.00000
...depending on whether the decimal is positive or negative. Is there an easy way to do this with the C# string.Format or myObj.ToString("[format here]") functions?
EDIT: Notice the extra space in the format string. This makes the strings the same length by adding a leading space when there is no negative sign.
Yes:
http://blog.stevex.net/string-formatting-in-csharp/
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
Here's a quick answer for what you posted above:
whateverstring.ToString(" 00.00000;-00.00000");
-29.69 -> "-29.69000"
48 -> " 48.00000" <-notice space padding the front.
The semi colon separated formatting string: "00.00;##.##" applies the 00.00 pattern to Positive and zero values, and the ##.## pattern to negative values.
In other words, what you had before for a formatting string works without any tweaking :-)
Yes, String.Format() will do anything VB Classic's Format function would do.
http://blogs.msdn.com/b/kathykam/archive/2006/03/29/564426.aspx

Categories

Resources