How do you expand a decimal with 0 or only 2 digits to 4 digits (#.0000)?

Viewed 45

I have decimals by different sources with different digits.

I need all of them like: #.0000 (with 4 digits)

decimal amountOrigin = 12345678.00; //automatically two digits via Newtonsoft.JsonConvert()
decimal amountByOwn = 1234567.0000; //I try to define by my own with 4 digits

decimal amount_A = Decimal.Round(amountOrigin, 4); //=> 12345678.00
decimal amount_B = Decimal.Round(amountOrigin + (decimal)0.000001, 4); //=> 12345678.0000
decimal amount_C = amountByOwn; //=> 1234567

amount_B is working, but not nice. :/

Is there a simple way to round or expand my decimals to 4 digits, even if the decimal does have less digits before? Like the String-Format, but I need it as a decimal, to calculate with them later.

I need the 4 digits because comparing hash values from different sources. And a md5 hash of 123.0000 is different of 123.00 or 123.0, ...

-------------------------------------------------------

Because my question was closed, here again my solution:

It shorts the zeros. So I can calculate with them later, but a md5 hash will be the same.

public string CreateMd5Hash()
{
    using (MD5 md5 = MD5.Create())
    {
        Md5Hash = BitConverter.ToString(md5.ComputeHash(Encoding.ASCII.GetBytes(
            ShopStatus +
            ApiCustomerId +
            CustomerFirstName +
            CustomerLastName +
            ApiCreditAccountId +
            Decimal.Round(new Decimal((double)CurrentAmount), 4) +
            Decimal.Round(new Decimal((double)OriginalAmount), 4) +
            Decimal.Round(new Decimal((double)RevokedAmount), 4) +
            OrderDate +
            DeliveryDate +
            InvoiceDate +
            ReversalDate +
            ReversalEntryDate +
            ReversalBookedDate
        ))).Replace("-", string.Empty).ToLower();
    }
    return Md5Hash;
}

Thanks for food for thought to @Olivier Rogier. :D

0 Answers
Related