C # Decimal numeric number (?,?)

I have a problem, I have not found a solution yet, so I ask for your help. In my database, I have int, decimal and string that need to be converted to numeric with a certain length and precision to be included in a flat file.

Example:

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

You cannot put a comma or period. Is there a simple solution? I try a lot of things, but no one will give me my result, except that I do foreach loops. Filed in my flat file is too dirty.

EDIT:

a flat file receives information based on the starting point and length, so all data included in the file must be cetain. And for Numeric, I have an example

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

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

+3
2

:

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

:

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

:

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));
+4

Source: https://habr.com/ru/post/1714201/


All Articles