how to convert string to integer in RDLC - c#

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

Related

Convert decimal to unsigned decimal (or string?) in C#

I have a variable, DifferenceAmt, which is a decimal. It can be either negative or positive. I need to output the value of DifferenceAmt, but without the negative sign if it's negative, as a string. I know how to delete the negative sign by checking first if DifferenceAmt is less than zero, and then doing a substring from the 2nd character if it is, but this seems cumbersome. I've tried converting DifferenceAmt to a UInt64 first,
UInt64 differenceamt = (UInt64)DifferenceAmt;
but I keep getting an error message that the number is too big for a UInt64 (even when it's -10, for example). How can I do this?
There are multiple available ways to do it.
The simplest way is to use absolute value using Math.Abs.
decimal x = -10.53m;
string s = Math.Abs(x).ToString(); // 10.53
Another way is to use custom NumberFormatInfo with empty NegativeSign.
decimal x = -10.53m;
string s = x.ToString(new NumberFormatInfo { NegativeSign = string.Empty }); // 10.53
One more way is to use custom formatter for positive/negative numbers. But you have to specify custom string format in such case. More on this: The ";" Section Separator.
decimal x = -10.53m;
string s = x.ToString("#.##;#.##") // 10.53
Use Math.Abs(DifferenceAmt).ToString(); This will give you the absolute value of the number and remove the negative sign.
Use Math.Abs(differenceAmt) to get the absolute value.
Casting to UInt64 won't work. Your example of -10 is outside the range of UInt64, and it would similarly fail for other Decimal values outside the range of UInt64. It would also have to truncate the value to an integer.
You can use this:
Math.Abs(differenceAmt).ToString();
see this:
http://msdn.microsoft.com/en-us/library/a4ke8e73(v=vs.110).aspx

simple int to hex conversion not working

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.

Trying to convert first symbol of string to int, getting weird value

static void Main(string[] args)
{
string str_val = "8584348,894";
//int prefix = Convert.ToInt32(str_val[0]); //prefix = 56 O_o
//int prefix = (int)str_val[0]; //what, again 56? i need 8!
int prefix = Convert.ToInt32("8"); //at least this works -_-
}
Any idea how to convert first symbol to right numeric value?
If you use:
Convert.ToInt32(str_val[0]);
then you are actually calling the overload:
Convert.ToInt32(char val);
which gives the Unicode/Ascii number of character being passed as a parameter.
If you want to convert first character, you need to force it to be a string type:
Convert.ToInt32(str_val.Substring(0, 1));
This way you call the overload:
Convert.ToInt32(string val);
which actually do what you want (convert string value to int value that this string represents).
Your trying to parse a string but passing in a char. Convert the character to a string first.
int prefix = Convert.ToInt32(str_val[0].ToString());
So the character value of 8 is the ASCII value 56, what you want to do is inteprete the value as a string rather than an ASCII Value. By using the .ToString() method you are converting the character into a null terminated string, which can be read by the ToInt32 method.
By doing this way Convert.ToInt32(str_val[0]) you are reading the character at a index of the string that converts the char to int. the int is the ascii for it
Like Bridge mentioned, it's getting the ASCII value. When you use the indexer on the string class to get just a character, it returns it as a char. If you read the char documentation you'll see that it is internally stored as a numeric UTF-16 value. It also has implicit conversions to and from most numeric types, which extract the UTF-16 value, or convert the numeric form into the character form. That's what you're doing.
What you mean to do is parse it as an int, not get the numeric representation of the UTF-16 value. That's where the Convert answers all come in.
Int32.Parse(str_val[0])
Will give you number in string.

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');

C# Get exact string formation from 'double' type

As I'm working on C#, I have one field named 'Amount'.
double amount = 10.0;
So, I want the result like '10.0' after converting it to string.
If my value
amount = 10.00, then I want result '10.00' after converting it to string.
So, Basically I want exact result in string as it is in double type. (With precisions).
Thanks in advance.
string result = string.Format( "{0:f2}", amount );
What you ask is not possible. A double in C# is a simple 64-bit floating-point value. It doesn't store precision. You can print your value with one decimal places, or two, as other answers describe, but not in a way that's "preserves" the variable's original precision.
string amountString = amount.ToString("N2");
"N2" is the format string used as the first parameter to the .ToString() method.
"N" stands for number, and 2 stands for the number of decimal places.
More on string format's here:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
As #Michael Petratta points out, double doesn't carry with it the precision of the input. If you need that information, you will need to store it yourself. Then you could reconstuct the input string doing something like:
static public string GetPrecisionString( double doubleValue, int precision)
{
string FormattingString = "{0:f" + precision + "}";
return string.Format( FormattingString, doubleValue);
}

Categories