I came across a code. Can anybody shed a bit of light on it. Plz be kind if anybody finds it a bit basic.
string str= String.Format("{0,2:X2}", (int)value);
Thankyou for your time.
X format returns Hexadecimal representation of your value.
for example String.Format("{0:X}", 10) will return "A", not "10"
X2 will add zeros to the left, if your hexadecimal representation is less than two symbols
for example String.Format("{0:X2}", 10) will return "0A", not "A"
0,2 will add spaces to the left, if the resulting number of symbols is less than 2.
for example String.Format("{0,3:X2}", 10) will return " 0A", but not "0A"
So as result this format {0,2:X2} will return your value in Hexadecimal notation appended by one zero from the left if it is only one symbol and then appended by space from the left if is it one symbol. After reading this several times, you can see, that ,2 is redundant and this format can be simplified to {0:X2} without changing the behavior.
Some notes:
: separates indexing number and specific format applied to that object. For example this code
String.Format("{0:X} {1:N} {0:N}", 10, 20)
shows, that I want to format 10 (index 0) in hexadecimal then show 20 (index 1) in numerical way, and then also format 10 (index 0) in numeric way.
0,2 from the left part of semi-column indicated index position 0 and format ,2 applied to the resulting string, not to a specific object. So this code
String.Format("{0,1} {1,2} {0,4}", 10, 20)
will print first number with at least one symbol, second with at least two symbols and then again first number with at least four symbols occupied. If the number of symbols in resulting string will be less - they will be populated by spaces.
{0,2:X2}
It splits into
0,2 - Format a number 10 into 10
X2 - Formats a number 10 into hexadecimel value 0A.
Update
Code
String.Format("{0,2:X2}", (int)value); // where value = 10
Result: 0A
Live Example: http://ideone.com/NW0U26
Conclusion from me
You can change "{0,2:X2}" to "{0:X2}", live example here.
Reference Links: MSDN
According to MSDN, a format string has the following format:
{index[,alignment][:formatString]}
We can find all of these components (the last two being optional) in your format string:
0 is the index of the parameter to use.
,2 is the alignment part, if the result is shorter than that, it is padded left with spaces.
:X2 is the formatString part. It means the number will be formatted in hexadecimal (uppercase) format, with a minimum width of 2. If the resulting number has less than 2 digits, it is padded with zeroes on the left.
In this specific case the alignment specifier is redundant, because X2 already specifies a minimum width of 2.
See here for more info on the format string:
Composite Formatting
Standard Numeric Format Strings
Related
I am trying to build a custom format specified for doubles for a two line element (tle) for space objects. From the wiki documentation TLEs
Where decimal points are assumed, they are leading decimal points. The last two symbols in Fields 10 and 11 of the first line give powers of 10 to apply to the preceding decimal. Thus, for example, Field 11 (-11606-4) translates to −0.11606E−4 (−0.11606×10−4).
This field is 8 characters long. First character is +/-/' ' followed by 5 numeric values (No zero padding) followed by a '-' and a single exponent value.
Does anyone know how to build this inline? ie $"{val,someFormat}" This would be preferred however I don't think it is possible so the alternative would be composing it of several pieces like
$"{val<0?"-":" "}{frac(val)}-{getExp(val)}".
Both frac() and getExp() need to be built, but my biggest problem is how to get the exponential value of the double. Is there any built in function that will return an int value of the exponent of a double? With that I think I can build everything else.
Again if there is an easier way I am all ears!
Thanks
Can someone help me out ?
How do I print out the decimals of a number to a certain number of decimals in C# or should i say, how do you add trailing zeros to meet the specified number.
Example: printing to 7 decimals
5.66 should return 0.6600000
0.123456 should return 0.1234560
A simple way to specify the number of digits is to use a custom formatting string. '0' is a placeholder for a digit to always print, '#' would be an digit to print if relevant. So 7 decimals would be "0.0000000", There are also standard formatting strings that may be useful.
If you are not interested in the whole number part you can just subtract it:
var decimalPart = myValue - (int)myValue;
var str = decimalPart.ToString("0.0000000");
i found the solution. You use the float function.
int double= Convert.ToDouble(Console.ReadLine());
Console.WriteLine($"{num:fn}");
f specifies a float
n specifies the number of decimal places.
so f4 = to 4 decimal places
Why does the following program not output the negative sign for the second line?
var smallpos = 3.65433E-005;
var smallneg = -3.65433E-005;
Console.WriteLine("{0} in F4 format with a width of 8 characters {1}",
smallpos,
smallpos.ToString("F4").PadLeft(8).Substring(0, 8));
Console.WriteLine("{0} in F4 format with a width of 8 characters {1}",
smallneg,
smallneg.ToString("F4").PadLeft(8).Substring(0, 8));
Using VS 2017 Professional 15.8.2 C# 7.2
-3.65433E-005 represents -0.0000365433.
The issue here is with smallneg.ToString("F4"). It only considers the first 4 places after the decimal point; since they're all 0 the negative sign is left out as -0 wouldn't make much sense.
Once you've eliminated enough digits from the back, the remaining number will have a value of zero, at which point the existence of a "-" has no real meaning.
This can be understood quite intuitively by running the snippet below, which has a decreasing number provided for the fixed-point specifier.
In the last line, that specifier is omitted, at that point NumberFormatInfo.NumberDecimal decides the number of decimal places used (depending on the culture used):
var smallneg = -3.65433E-005;
Console.WriteLine(smallneg.ToString("F10")); // -0,0000365433
Console.WriteLine(smallneg.ToString("F9")); // -0,000036543
Console.WriteLine(smallneg.ToString("F8")); // -0,00003654
Console.WriteLine(smallneg.ToString("F7")); // -0,0000365
Console.WriteLine(smallneg.ToString("F6")); // -0,000037
Console.WriteLine(smallneg.ToString("F5")); // -0,00004
Console.WriteLine(smallneg.ToString("F4")); // 0,0000 <-- Zero --> (-0) == 0
Console.WriteLine(smallneg.ToString("F")); // 0,00
I am tring to convert a wdColor to rgb # color by this flowing code. I am converting the enum wdColor result to hex by ToString("x6").But Sometimes it gives me back a 8 lenght string, and i need 6 length string to convert it to rgb;
var num = -603914241;
var numToHex = y.ToString("x6");
gives "dc00ffff" that has 8 charectars.
The input number is too big to be represented using just 6 characters. x6 means that the output should be at least 6 characters long, padding by zeros as necessary to meet that minimum length - but if the input is too big then it'll use as many characters as necessary to represent it.
According to manual
https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#XFormatString
The hexadecimal ("X") format specifier converts a number to a string
of hexadecimal digits. The case of the format specifier indicates
whether to use uppercase or lowercase characters for hexadecimal
digits that are greater than 9. For example, use "X" to produce
"ABCDEF", and "x" to produce "abcdef". This format is supported only
for integral types.
The precision specifier indicates the minimum number of digits desired
in the resulting string. If required, the number is padded with zeros
to its left to produce the number of digits given by the precision
specifier.
In your case it means that x6 guarantees at least (not exactly) 6 hexadecimal digits
I want to display a three bytes (signed) in C#.
The code (fragment) I made is:
case 3:
HexadecimalValueRange = SignedValueChecked ?
string.Format("{0:X6}..{1:X6}", (Int32) minValue, (Int32) maxValue)
: string.Format("{0:X6}..{1:X6}", (UInt32)minValue, (UInt32)maxValue);
But it displays an example negative value as 0xFFC00000 where I would like to see 0xC000000, so with 6 'significant' digits (thus without the leading FF).
The leading bits of negative number are significant, so you can't cut the off with String.Format (there is no specifier that let you ignore significant digits, width specify only minimum size and left/right justification).
You can convert the values to 3-byte uint to print them the way you want:
string.Format("{0,-6:X}",(uint)(int.MaxValue & 0xFFFFFF))