1

In the below code i want to add 0 before the number (ie) if the number is 96333 it should be 096333 when i execute it i am facing issue cannot convert long to int.Pls help me to solve the issue.

  public FileDto GenerateBarcodeImage(Int64 BarCodeNumber)
    {
        //string s = BarCodeNumber.ToString().PadLeft(4, '0');
        string result = BarCodeNumber.ToString().PadLeft(4, '0');
        int sds = Int64.Parse(result);
        return GetBarcodeImage(sds);
    }
fubo
  • 44,811
  • 17
  • 103
  • 137
Moses E
  • 25
  • 1
  • 1
  • 6
  • What is the exception being thrown? – Dai Apr 02 '15 at 09:45
  • `Int64.Parse("096333")` will return you `96333 `, You will lose all zero's on left so whats the use of converting – Satpal Apr 02 '15 at 09:45
  • 2
    By parsing back to Int64, you lose all zeroes to the left. Those are just formatting, not representative of your actual integer value. – Mr47 Apr 02 '15 at 09:45
  • I don't know what GetCodeBarImage is suppose to do but it looks wrong working with a int instead of a string if you want to display those zeros – Emmanuel M. Apr 02 '15 at 09:47
  • possible duplicate of [How to convert long to int in .net?](http://stackoverflow.com/questions/5192862/how-to-convert-long-to-int-in-net) – Bolu Apr 02 '15 at 09:48

3 Answers3

1

replace

int sds = Int64.Parse(result);

with

long sds = Int64.Parse(result);
fubo
  • 44,811
  • 17
  • 103
  • 137
1

long is more precise than int. An implicit cast can be done only from lower to bigger precision, because there is no risk of loosing information (because the smallest and biggest values of int can be easily contained in a long).

If your value may be bigger than the capacity of int, change the type of sds to Int64. I suppose your function GetBarcodeImage takes an Int64 as parameter, if not you may want to change that too.

If your value can be contained in an integer, juste change your GenerateBarcodeImage function to take and int as parameter.

Think carefully to the consequences of casting a number to a lower precision. Read the story of the first Ariane 5 launch for more details.

Florent Henry
  • 702
  • 1
  • 7
  • 25
0

Try this:

int sds = Int32.Parse(result);

instead of

int sds = Int64.Parse(result);

As correctly pointed by Mr47 when you are parsing back the number to Int64, you will lose all zeroes to the left.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331