DEV Community

Daily Challenge #310 - Boolean to String Conversion

dev.to staff on January 21, 2021

Implement a function which will convert the given boolean value into its string representation. So for the boolean false, return "false" and for t...
Collapse
 
bradtaniguchi profile image
Brad •

shortest version I can think of:

javascript

const boolToStr = b => '' + b;
Enter fullscreen mode Exit fullscreen mode

or a more safe version that wont print null or undefined, rather it will convert it to true/false depending on its "truthiness":

javascript

const boolToStr = b => '' + !!b;
Enter fullscreen mode Exit fullscreen mode
Collapse
 
chaugiang profile image
Nguyen Tran Chau Giang • • Edited

My solution that is submitted to Codewar

const char *boolean_to_string(bool b)
{
    return b ? "true" : "false";
}
Enter fullscreen mode Exit fullscreen mode

My another solution that is accepted int and char type.
C using _Generic. feel free to contribute

#define pt() printf("true\n")
#define pf() printf("false\n")
#define cast_to_boolean (x) _Generic( (x), char: x == '\0' ? pf() : pt(), int: x == 0 ? pf() : pt(), default: pf())
Enter fullscreen mode Exit fullscreen mode
Collapse
 
qm3ster profile image
Mihail Malo • • Edited

Rust

1. return an owned String:

format!("{}", v);
Enter fullscreen mode Exit fullscreen mode

or (on nightly)

format!("{v}");
Enter fullscreen mode Exit fullscreen mode

or

v.to_string();
Enter fullscreen mode Exit fullscreen mode

2. return a reference to a string embedded in the binary:

fn bas(v: bool) -> &'static str {
    match v {
        true => "true",
        false => "false",
    }
}
Enter fullscreen mode Exit fullscreen mode

someone in discord golfed that to

["false", "true"][my_bool as usize]
Enter fullscreen mode Exit fullscreen mode

but idk idk :v

Collapse
 
nijeesh4all profile image
Nijeesh Joshy • • Edited

Ruby

there already exists a function to_s.

If you want another one, here it is

def bool_to_s(str)
  (!!str).to_s
end
Enter fullscreen mode Exit fullscreen mode

It will work without the !! I just wanted to make sure it will work even if anything else is passed

Collapse
 
aminnairi profile image
Amin • • Edited

JavaScript

const describeBoolean = boolean => `${boolean}`;
Enter fullscreen mode Exit fullscreen mode
Collapse
 
raphaelchaula profile image
Raphael Chaula •

JavaScript

const booleanToString = (value) => {
    return JSON.stringify(value);
};

const booleanValue = true;
const itsStringValue = booleanToString(booleanValue);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mellen profile image
Matt Ellen-Tsivintzeli •

That's pretty heavyweight! value.toString() works, too.

Collapse
 
raphaelchaula profile image
Raphael Chaula •

Yes, mine is really really heavy, just tried something no one could think of.

Collapse
 
juanbenitezdev profile image
Juan Benitez •
boolean_to_string = lambda x: str(x)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
gargakshit profile image
Akshit Garg •
package main

import (
    "fmt"
    "strconv"
)

func main() {
    myBool := true
    fmt.Println(strconv.FormatBool(myBool))
}
Enter fullscreen mode Exit fullscreen mode

Some go here

Collapse
 
peter279k profile image
peter279k •

Python:

def boolean_to_string(b):
    return str(b)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
krusty93 profile image
Andrea Grillo •

C#
bool.TrueString.ToLower()
bool.FalseString.ToLower()