textbox with arabic language - c#

I have a problem with a textbox. the problem is that I initialized the property
textBox1.RightToLeft = RightToLeft.Yes;
and when I typed arabic characters it types correctly but the problem if I type the following
string
66/ج ح /
96
the number 96 after the / but the editor doesn't allow to write it that way so when I read it it change its characters like this :
ج ح/66/96

var LRM = ((char)0x200E).ToString();
lbldate.Text = ddlTrnNoYears.SelectedItem.Text+'/' + LRM + ddlTrainingMonth.SelectedItem.Text + LRM +'/'+ddlTrainingDay.SelectedItem.Text;
Result :
۳۹۹/‎مرحبا‎/۳

Related

C#, label ignores parts of the given text

i need to put in my label something like "x rub | y%" but label ignores the rest of text after the x.
when the label gets "0" value, it works as i want it to, i.e. "0$. | 0%", but whenever the value isnt 0, it only displays "read_cat"'s value ("2000"). Thank you in advance.
System.IO.StreamReader reading2 = File.OpenText("categories\\clothes.txt");
read_cat1 = Convert.ToString(File.ReadAllText("categories\\clothes.txt"));
clothes_percentage.Text = Convert.ToString(read_cat1) +"$ | "+
Convert.ToString(Math.Round(Convert.ToDouble(read_cat1) * 100 / salary, 2))+"%";
reading2.Close();
My guess is that your file contains a linebreak after the actual value. Use the Trim() method to remove linebreaks and other whitespace from the string you read from the file.
string read_cat1 = File.ReadAllText("categories\\clothes.txt");
read_cat1 = read_cat1.Trim();
clothes_percentage.Text = read_cat1 + "$ | " +
Math.Round(Convert.ToDouble(read_cat1) * 100 / salary, 2) +"%";

Show double as percentage without decimals with ToString method

Looking for:
95,4545454545455 -> 95 %
I tried using:
String resultAsPercentage = result.ToString("##0 %");
But, it shows
9545 %
Then, I solved my problem using regex:
Question: Why my ToString method hasn't worked? And how to fix it to avoid using regex?
Thanks in advance.
As documented on Custom Numeric Format Strings, the % modifier multiplies the value by 100 before inserting the %. It's intended to be used with fractions. To disable this special meaning of %, escape it by preceding it with #"\".
Alternatively, you could take the % out of the format string, and append it manually: result.ToString("##0") + " %".
If you don't care about rounding, you can use the following:
double result = 95.4545454545;
String resultAsPercentage = (int)result + " %";
System.out.println(resultAsPercentage);
Output is: 95 %
Casting to an int drops the decimal places without rounding
You can use thew P(ercentage) format specifier, you need to divide through 100 because the specifier multiplies it by 100:
decimal value = 95.4545454545455m;
String resultAsPercentage = (value / 100).ToString("P0"); // 95%
If you need the space between the value and the percentage symbol you could use this approach:
NumberFormatInfo nfi = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
nfi.PercentSymbol = " %";
String resultAsPercentage = (value / 100).ToString("P0", nfi); // 95 %
One way can be Clone a culture (like InvariantCulture), set it's PercentPositivePattern to 0, divide your value by 100 and get it's string representation using The percent ("P") format specifier with 0 precision and that cloned culture as;
var clone = (CultureInfo)CultureInfo.InvariantCulture.Clone();
clone.NumberFormat.PercentNegativePattern = 0;
Console.WriteLine(((int)95.4545454545455 / 100.0).ToString("P0", clone)); // 95 %
You can see all associated patterns on Remarks section on that page.
You can guaranteed to set PercentNegativePattern property as well for negative values.

Line up Characters

I have a combobox made up of two numbers; inches and millimetres. At the moment it is looking hideous. I am wondering if some of the gurus here have anyway of lining the character '|' or at least make it nicer?
A bit of background info, the number inches and millimetres are separate strings which I append together like so:
Size(in) + " (In) | " + Size(mm) + " (mm)"
Possibly the cleanest way would be to format every number to have 3 decimal places for at least inches. This still won't be perfect however since the letter font width won't be perfect, to fix that you'd need to use a monospaced font.
To format to 3dp you can use the following
String.Format("{0:f3}", Size(in)) + " (In) | " + Size(mm) + " (mm)"
Since you have some values that are 2 digits before the decimal you can always use PadLeft to align these, but again this doesn't always work well without a monospaced font..
String.Format("{0:f3}", Size(in)).PadLeft(5, ' ') // or (5, '0')
Use String.PadRight(i); and String.PadLeft(i); where i is a nr. of spaces to "fill":
Example:
// Just to simplify a little, create vars:
var inches = Size(in) + " (In) ";
var mm = " + Size(mm) + " (mm)";
var formatted = inches.PadRight(15) + "|" + mm.PadLeft(15);
Example of output using 15 for the padding value (obviously, you can adjust this as needed):
43 inches | 123 cm
445554 inches | 12345 cm

Convert MS Access Color Code to Hex in C#

Is there a method to convert MS Access Color codes to Hex in C#?
e.g.
- (white) 16777215 -> #FFFFFF
- (black) 0 -> #000000
- (blue) 16711680 -> #0000FF
Here are some reference tables I found on stackoverflow
http://www.endprod.com/colors/invcol.htm
http://cloford.com/resources/colours/500col.htm
You can convert to hex like so:
string hexValue = "#" + 16777215.ToString("X");
Or wrap it up in a method:
public static string AccessToHex(int colorCode) {
return "#" + colorCode.ToString("X");
}
You need to convert the value to hexadecimal, then flip the first two digits with the last two. For example, converting the raw value of 16711680 for blue gives an hex value of FF0000. However, the value for blue is 0000FF; a swap is required (So yes, the other answer is wrong...)
The value is also padded to always have the 6 required digits.
string rawHex = msAccessColorCode.ToString("X").PadLeft(6, '0');
string hexColorCode = "#" + rawHex.Substring(4, 2) + rawHex.Substring(2, 2) + rawHex.Substring(0, 2);
To do the reverse (hex -> Ms Acces), simply do the steps the other way around. Strip the extra # character, flip back the first/last two values and convert that number from base 16 to base 10.
string input = "#0000FF";
string hexColorCode = input.TrimStart('#');
string rawHex = hexColorCode.Substring(4, 2) + hexColorCode.Substring(2, 2) + hexColorCode.Substring(0, 2);
string result = Convert.ToInt32(rawHex, 16).ToString(); //16711680
Please note that Intew.Max is set to 0x7FFFFFFF (And our color codes cap at 0xFFFFFF), so it's completely safe to use Convert.ToInt32 here instead of Int64.

Format decimal value to string with leading spaces

How do I format a decimal value to a string with a single digit after the comma/dot and leading spaces for values less than 100?
For example, a decimal value of 12.3456 should be output as " 12.3" with single leading space. 10.011 would be " 10.0". 123.123 is "123.1"
I'm looking for a solution, that works with standard/custom string formatting, i.e.
decimal value = 12.345456;
Console.Write("{0:magic}", value); // 'magic' would be a fancy pattern.
This pattern {0,5:###.0} should work:
string.Format("{0,5:###.0}", 12.3456) //Output " 12.3"
string.Format("{0,5:###.0}", 10.011) //Output " 10.0"
string.Format("{0,5:###.0}", 123.123) //Output "123.1"
string.Format("{0,5:###.0}", 1.123) //Output " 1.1"
string.Format("{0,5:###.0}", 1234.123)//Output "1234.1"
Another one with string interpolation (C# 6+):
double x = 123.456;
$"{x,15:N4}"// left pad with spaces to 15 total, numeric with fixed 4 decimals
Expression returns: " 123.4560"
value.ToString("N1");
Change the number for more decimal places.
EDIT: Missed the padding bit
value.ToString("N1").PadLeft(1);
Many good answers, but this is what I use the most (c# 6+):
Debug.WriteLine($"{height,6:##0.00}");
//if height is 1.23 => " 1.23"
//if height is 0.23 => " 0.23"
//if height is 123.23 => "123.23"
All above solution will do rounding of decimal, just in case somebody is searching for solution without rounding
decimal dValue = Math.Truncate(1.199999 * 100) / 100;
dValue .ToString("0.00");//output 1.99
Note the "." could be a "," depending on Region settings, when using string.Format.
string.Format("{0,5:###.0}", 0.9) // Output " .9"
string.Format("{0,5:##0.0}", 0.9) // Output " 0.9"
I ended up using this:
string String_SetRPM = $"{Values_SetRPM,5:##0}";
// Prints for example " 0", " 3000", and "24000"
string String_Amps = $"{(Values_Amps * 0.1),5:##0.0}";
// Print for example " 2.3"
Thanks a lot!

Categories

Resources