10% of Random Number with only 2 decimal places

Hi Guys,

The 1023.91 is produced from
Decimal.Parse(Math.Round((New Random).NextDouble()*99.99+999.99,2).ToString)
Sub Total = £1023.91

The £102.391 is produced from ( basically 10% of the Sub Total )
string.Format(“Shipping = £{0}”, (SubTotal*Shipping10Percent).ToString)
Shipping = £102.391

Im looking to have the shipping total with only 2 decimals.

Thanks guys :grinning:

grafik
grafik

the digit in the format string will define the length for the decimals

Hello @MikeC

Does that mean you need the result as £102.40 ??

Yes Peter that is correct, 2 digits in the format string gives me lets say 1023.91
The issue I have is 10% of this random number.
So 10% of 1023.91 = 102.39, instead im getting 102.391 which is technically correct but I just want 2 decimal places.

Thank you.

Ermmmm not necessarily :joy:

10% of £1023.91
= £102.39
Or £102.40 (rounded up)

Thanks :grinning:

not clear as you can also cut / get returned 2 digit length decimal in the same way. Maybe we havent something not understood from your case

Decimal.Parse(Math.Round((New Random).NextDouble()*99.99+999.99,2).ToString)
Produces 1023.91
Above is perfect, gives me 2 decimal places.

string.Format(“Shipping = £{0}”, (SubTotal*Shipping10Percent).ToString)
Basically this gives me 10% of the 1023.91
Produces 102.391
Ideally I would like above to be 2 decimal places, not 3.

I would like either 102.39 or 102.40 (rounded up )

Makes sense I hope, cheers buddy :smile:

still confused as we created it with
grafik

Whats that F2 ? :thinking:

1 Like

Yehhhhhhhaaaaaawwwwwwww :heart_eyes:

(SubTotal*Shipping10Percent).ToString(“F2”)

Never ever used that (“F2”) before, learn something new every day.
I noticed (“F”) gives the same output as (“F2”)
Cheers Peter, got there in the end .

Screenshot 2022-08-02 202649

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.