11/15/2023 0 Comments Convert string to integer![]() in a new language's stdlib), a good rule of thumb is to treat invalid inputs in the highest-level way available, typically exception. I don't think a more concrete answer would truly be more useful here, but just in case that's all you're after: If we know we're designing a function for general use (e.g. ![]() If it will be called a jillion times in a super-tight loop across 10k cores (think Google scale), sometimes the right decision is to write other code so it never passes in invalid inputs.Īnd two or three of those choices make f("") return 0, assuming "" is invalid input in the first place. Throwing an exception is obviously a common choice in the modern world, but depending on how the function will be used, any of those could make sense. completely undefined behaviour (no promises - might corrupt memory or crash).an undefined return value (but at least a return value).a well defined best-effort result (converting "123abc" to 123).a special "error" int return that is known to be invalid or highly unlikely in the domain the function is dealing with (e.g.a standard way of marking the absence of a value (null return, "optional"/"maybe" return type, separate return-presence indicator).a standard way of signaling an error (exception, separate status return).A well-written function might promise to handle invalid input with: Likewise, though we don't customarily represent the zero-value with an empty string, it can be a perfectly reasonable choice to declare that this function considers "" a representation of zero. These are really two separate (though not independent) design decisions:Ībout that first decision, any of "+3", "03", "0x3", "3.0", "3.", "3e0", "3 ", " 3" could in some domain reasonably be declared a valid or invalid int representation. Of course it depends on how the function is meant to be used. ![]() If you document what your conversion function does, then it will not (or at least should not) surprise the clients of your function. If there are no implementations for your situation, then look at other languages or libraries for inspiration of how to handle those cases in your own implementation. So all in all the actual answer to your question should be to use the string-to-number conversion library functions supplied with your current language and framework, if they have any. You can probably come up with a few more if you tried.If you answer yes to the previous question, what rounding or truncation will you use?.Will you consider converting strings representing floating point numbers to integer numbers?.What string number representations will you accept? There is more than decimal - the following strings are also integer number representations in common use: 0x0A, 2x10^2, 10E5, 08.This is tricky - consider the following additional cases: The question behind the question seems to be "how to design a robust non-surprising string-to-int conversion utility". An empty string doesn't represent any integer, so at face value your question has a trivial answer - do not convert an empty string to a valid integer.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |