![]() ![]() So do you know a better solution? Then share it with me. ![]() I want to assign the correct answer as 1 and 2 as the wrong answer. You have 'boolValue true ' To String: //convert to the string 'TRUE' string StringValue boolValue.ToString And back to boolean: //convert the string back to boolean bool Boolvalue Convert. I've already converted it into a string but need to make a boolean from the answer. And then convert the boolean value between Boolean and String before/after saving/reading the value from the database. I think there aren’t much paths to get this effect in a more elegant way. I am trying to create a boolean from an Edit Text field. With three characters from string (with the value “0” or “1”) to Boolean. So the solution for strings looks like this: Its static final fields FALSEand TRUE representsa Boolean object that wraps the. Then use the not-operator as shown above. In addition, the toStringmethod converts the value to a String. Using the + operator you can cast the strings to int. “0”- and “1”-Strings to true and falseĪnd what if 0 and 1 are represented as strings, for example if you have to handle bad formatted JSON string? Nothing could be easier. With a second ! (so !!1) we negate the resulting false and get thus true for the 1. When we write !1 now, then we create a false condition because 1 isn’t false. Use parseBoolean(String) to convert a string to a boolean primitive, or use valueOf(String) to convert a string to a Boolean object. Why it works? Here is a short explanation. To convert the integers 0 and 1 to boolean, it is sufficient to use the not-operator twice. So this is the stuff I like to add to my personal “POP”-collection (pearls of programming)… 0 and 1 to true and false In addition, it works not only for integer values, but also for the string representations of 0 to 1. I think it is probably the shortest and most elegant way to go. The following way to reach the wanted result I’ve seen today. There are certainly many ways to convert 0 and 1 in Javascript to true and false. Today there will be only a very short post. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |