2

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

Froschkoenig84
  • 566
  • 4
  • 13
  • the problem is, I need a decimal and in the end I create a md5 hash to compare some fields in my obejcts. – Froschkoenig84 Aug 10 '21 at 03:35
  • 1
    You can create md5 hash of the string representation of the number and that should result in the same value... – Chetan Aug 10 '21 at 03:36
  • 1
    @Froschkoenig84 See the accepted answer of the suggested duplicate. I'm really disappointed to learn more about this `decimal` which I never actually use, and which is not related to the double and the float at all... it's really amazing that such a thing can hold the length of the decimal part as requested in your question. Good to know! I now finally understand why it is not part of the list of [integral types](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/integral-numeric-types). –  Aug 10 '21 at 03:39
  • `decimal amount = new Decimal((double)amountOrigin);` is a way to short/reduce all zeros. - Maybe a solution for me. - So I can calculate with them and have the same digits for my hash compares. – Froschkoenig84 Aug 10 '21 at 03:42
  • 1
    As Olivier's linked question has multiple solutions to your problem ([this one](https://stackoverflow.com/a/13770007/3181933), or [this one](https://stackoverflow.com/a/13769997/3181933), for example), I have marked yours as a duplicate of that question. Personally I would suggest `amount_B.ToString("F4", CultureInfo.InvariantCulture);` (you'll need to include `using System.Globalization;` at the top of your file) as the culture ensures the decimal separator is a `.` and not a `,`. – ProgrammingLlama Aug 10 '21 at 03:52
  • 2
    To expand the decimal to specific amount of decimals, simply add zero decimal constant expanded to the desired decimals. Apply `Round` to handle inputs with more decimals. e.g. `var amount4 = Decimal.Round(amountOrigin + 0.0000m, 4)` – Ivan Stoev Aug 10 '21 at 04:01

0 Answers0