Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

290
Views
What is the minimum decimal which can be converted to float and then back to decimal?
var t = (decimal)((float)decimal.MinValue)

fails with error CS0031: Constant value '-7.922816E+28' cannot be converted to a 'decimal', but why?

decimal.MinValue is -79228162514264337593543950335m. It successfully converts to float as -79228160000000000000000000000f wichh is larger than decimal.MinValue:

(float)decimal.MinValue > decimal.MinValue

Why this value can't be converted? I understand it's something about the significand and the exponent but what exactly happen? And which is minimum decimal which can be converted to float and then back to decimal in C#?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You are not printing the full precision of the float, so you cannot see the value that is causing it to fail.

If you change the code as follows:

var decMin = (float)decimal.MinValue;
Console.WriteLine(decMin.ToString("R")); // "-7.9228163E+28"

// Next line throws System.OverflowException: Value was either too large or too small for a Decimal.
var dec = (decimal)decMin; 

The value of decMin is displayed as -7.9228163E+28. To make this easier to compare, let's rewrite that without the exponent and compare it to decimal.MinValue:

decMin           = -79_228_163_000_000_000_000_000_000_000
decimal.MinValue = -79_228_162_514_264_337_593_543_950_335

Now you can see that decMin is MORE NEGATIVE than decimal.MinValue - which explains why it fails to convert.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!