Question

I have a problem, i didn't found yet the solution so i am asking your help. In my database I have some int, decimal and string that needs to be convert in numeric with specific length and precision to include in a flat file.

ex:

integer 123 to numeric(8,0) ==> 00000123
decimal 123,123 to numeric(8,8) ==> 0000012312300000
String "22" to numeric(8,0) ==> 00000022

It can't put comma or dot. Is there an easy solution I try a lot of things but none will give me my result except doing loops foreach Filed in my flat file too dirty!!

EDIT:

the flat file gets information based on there start point and the lenght so every data i includ in the file has to be a cetain lenght. And for the Numeric I have for exemple

   database Decimal Price = 123,456
   File     Numeric(8,6) Price = 00000123456000

I wanted to know how could i parse any decimal or integer data based on N(,)

Was it helpful?

Solution

Try this:

string ToNumericString(int value) {
    return value.ToString("00000000");
}

string ToNumericString(decimal value) {
    var value16 = Math.Truncate(value * 100000000);
    return value16.ToString("0000000000000000");
}

string ToNumericString(string value) {
    return ToNumericString(int.Parse(value, CultureInfo.InvariantCulture));
}

To call it:

    MessageBox.Show(ToNumericString(123));
    MessageBox.Show(ToNumericString(123.123M));
    MessageBox.Show(ToNumericString("22"));

or more general:

string ToNumericString(decimal value, int digitsBefore, int digitsAfter) {
    var value16 = Math.Truncate(value * (decimal)Math.Pow(10,digitsAfter));
    return value16.ToString(new String('0', digitsBefore + digitsAfter));
}

MessageBox.Show(ToNumericString(123.123M, 8, 3));

OTHER TIPS

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top