简体   繁体   中英

Restrict value on decimal TryParse

I have following code:

Decimal.TryParse("1.0e-50", NumberStyles.Float,CultureInfo.CurrentCulture.NumberFormat, out val) 

I want to restrict it to e-45. Is there a way to do this without using regular expressions? I googled it but only best way i got was regex.

Parse the value first and just compare it with a min/max value afterwards because there is no better way than regex to check how big the decimal value contained by the string is. All in all, parsing is much cleaner, efficient and I also would guess that it is faster.

You should really just parse it as a decimal without bounds checking it, and then check the min/max values after you've parsed it into a numeric value. Trying to do the bounds checking on the string, before parsing it, is just asking for trouble; it'll be way more work, and more error prone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM