simple int to hex conversion not working - c#

I have a very simple code where I get a value from numeric updown which I have then to convert to hex. (for numericupdown Hexadecimal property is set true)
I select FF from nud located in winForm. then in code
string str = nudID.Value.ToString("X");
But this is not working and I am getting format exception

NumericUpDown value isn't Int type, it's Decimal. Maybe here is the problem?

NumericUpDown.Value returns Decimal. Decimal.ToString(string) does not support "X":
The format parameter can be any valid standard numeric format specifier except for D, R, and X
Adapting some code from this solution, try this if you are on .NET 4.0 or later:
string str = new System.Numerics.BigInteger(nudID.Value).ToString("X");

Try this
string str = nudID.Value.ToString("X2");

this should work:
string str = ((int)nudID.Value).ToString("X");
You need to parse the decimal value to int. You can make it a bit safer by using TryParse if you want to.

Related

Remove Decimal form String if equals to ".00"

Im having alot of problems trying to take out the decimal part of my string,
the string comes from a var type in my view like this:
var temp = dashList[index];
#PrintSection(actualDate, Model, String.Format("{0:0.000}", temp.Rubro))**
temp.Rubro is my String part that can be ".00" or ".XX"
however i need to take the decimal part of the string only when its value is ".00"
since i have some values of the dashlist have important decimal parts.
Is there a way to take the decimal part of a string only if it equals to ".00"???
The output im trying to get is:
From XX.00 -> XX
From XX.12 -> XX.12
both kinds are on my list
Try this:
var temp = dashList[index];
#PrintSection(actualDate, Model, String.Format("{0:0.000}", temp.Rubro).Replace(".00", ""))
You can try using
String.Format("{0:G29}", decimal.Parse(temp.Rubro)))
Whereas all the below formats achieve the same results.
string.Format("{0:G29}", decimal.Parse("2.00"))
decimal.Parse("2.00").ToString("G29")
2.0m.ToString("G29")
You can use ToString() with the General ("G") Format Specifier to achieve the desired result. Trailing zeros are truncated when using this format string with a precision specified. In order to prevent rounding in any situations, you will need to set the precision to the maximum allowed for decimals (29).
Simple trick...
You can parse the string and it will automatically remove the decimal places
try following
private static void ParseDouble()
{
string sDouble = "12.00";
double dValue = double.Parse(sDouble);
Console.WriteLine(dValue.ToString());
sDouble = "12.14";
dValue = double.Parse(sDouble);
Console.WriteLine(dValue.ToString());
}
Your output will be
12
12.14
Hope this helps.
I will suggest you to use accounting.js plugging which is pretty straightforward. I have used it in some projects and as of today I cannot complain. Otherwise, you could do something like this,
var x = temp.Replace(".00","").Trim();

Format numeric string to another numeric string

As I saw in google, there are many implementation for string to an int or decimal... but not to a string.
For example:
decimal d1 = 32221.0210m;
Console.WriteLine(d1.ToString("#,###.00"));
This works but the problem is that I have a string and I don't want to cast it to a numeric.
string str = "32221.0210";
I want it to be:
str = "32,221.02";
Is it possible (without casting)?
For those who ask if is there any reason why not cast: Yes. I am using Infrastructures that receive a string. The infrastructures team don't want to convert it to decimal because they also may recieve another values such as date, etc...
In worse case, it is possible to TryParse decimal, so we know for sure it will work. But for now, we are trying to parse a string to a string as written above.
Thanks in advance :)
A temporary var of type decimal is required:
decimal d;
string str = decimal.TryParse("32221.0210", out d)? string.Format("{0:#,##0.00}", d):string.Empty;

how to convert string to integer in RDLC

I want to convert string number like "000023" to integer 000023 in RDLC report but when I am trying to convert it into integer it only displays 23 instead of 000023.
does anyone know how to do this ?
thanks
000023 is not an valid integer. When 000023 is converted to an integer, it will automatically be converted to the valid integer 23.
Jaq316's answer is correct.
How could you have an int as 000023? You could format it as a string with leading zeros which you have already it as a string.
But if you want to display 23 as a 000023 you can use String.PadLeft like;
public string Represent(string s)
{
int i = 0;
if (int.TryParse(s, out i))
{
return(i.ToString().PadLeft(6,'0'));
}
}
This method seems pointless of course since you have already 000023 in your database.
Integral types and Floating point types doesn't owns any formatting.
Formatting is done only when converting them to string. so you've to format it when you display it. There is no way to store an integer as 000023

Formatting values

Hi i have a int example as 3 i need to format it as 003 . is the only way is convert to a string and concat and convert back ?
I guess this is what you want:
int n = 3;
string formatted = n.ToString("000");
Alternatively:
string formatted = String.Format("{0:000}", n);
More info here.
You can apply the .ToString("000"); method.
Debug.WriteLine(3.ToString("000"));
You can parse the resulting string value by using int.Parse or int.TryParse:
Debug.WriteLine(int.Parse("003"));
See Custom Numeric Format Strings
If it's an int object, the leading zeros will always be removed, regardless if you convert it to a string and back.
use the pad functionint i = 1;
i.ToString().PadLeft(3, '0');

formatting string in C#?

I've a predefined string format. For instance '>>>,>>>,>>9.99' this means that the system should display string in this '500,000,000.10'. The format can change based on the users using it. How can I write a common function to display stings on the given format passing
the input value and the format as the parameter using C#
You can use the ToString method with a standard or custom format string
For example:
string format = "{0:000,000,000.00}";
string val = 12.3456;
Console.WriteLine(string.Format(format, value)); // it prints "000,000,123.23"
You can read more about formating values here http://www.csharp-examples.net/string-format-double/
decimal value = 1.2345;
string rounded = value.ToString("d2");
private string sDecimalFormat = "0.00";
decimal d = 120M;
txtText.Text = d.ToString(sDecimalFormat);
You could then have a setting for decimal format eg:
txtText.Text = d.ToString(Settings.DecimalFormat);
String.formate can be used for formating.
Go there if you want examples
http://www.csharp-examples.net/string-format-double/
I think the following might work:
String result = String.Format(fmt.Replace('>', '#').Replace('9', '0'), inpString);
fmt being the format you want to use and inpString being the string entered by the user.
Just replace the > with # and the 9 with 0 and it'll be a valid .Net formatstring.
There is a Format method on String.
String.Format("{0:X}", 10); // prints A (hex 10)
There are several methods to format numbers, date...
I dont seem to understand how you can make 500,000,000.10 from >>>,>>>,>>9.99' but I believe the answer would be
But I assume something you are looking for is: string.Format("500,000,00{0:0.##}", 9.9915)
You can then make a method like
Public string GetString(string Format, object value)
{
return string.Format(Format, value);
}
Something like this?

Categories