-1
Boolean Multiplier;
double Number;
double Value;
string Text;
Text = (Value * (1 + (Convert.ToInt32(Multiplier) / 2)).ToString(".0##");

This is the current code I have abstracted down to the general behaviour with the intended behaviour of Value being multiplied by 1.5 if Multiplier is true and Value staying the same if Multiplier is false.

However, this someone does not work although it worked without the division and I think it has to do with floats not being displayed correctly. So I wanted to ask wether I can use something like enums where I have two values and one of them gets chosen dependant on wether the boolean is true or false. Other ways are appreciated as well as advice on formatting decimal numbers on the side as I'm pretty new. If this is documented somewhere already, then I'm sorry but I did not found it :(

Firro
  • 21
  • 1
  • 6
  • 1
    You could use a [ternary](https://msdn.microsoft.com/en-gb/library/ty67wk28.aspx) statement `Value *= Multplier ? 1.5 : 1;` `Text = Value.ToString(".0##");` alternatively use an if with the same logic – Alfie Goodacre Dec 14 '16 at 17:25
  • Thanks, this is exactly what I wanted. You would have gotten the checkmark but sadly its a comment :/ – Firro Dec 14 '16 at 18:07
  • It wasn't really worth an answer so I didn't bother :P – Alfie Goodacre Dec 14 '16 at 19:14

2 Answers2

1

You could just use an if statement:

double val = Value;
if( Multiplier )
{
   val = val * 1.5;
}
Text = val.ToString( ".0##" );
clcto
  • 9,530
  • 20
  • 42
  • I dont want to use it this way because if I decide to add more Multipliers to the same formula I would have to make a nest of ifs and elses. – Firro Dec 14 '16 at 18:05
  • @Firro nesting `?` is much harder to read then nested `if`s. If you added multiple multiples the code of the accepted answer would look something like `Multiplier ? 1.5 : (Multiplier2 ? 2 : (Multiplier3 ? 3 : 1) )` and that is without the redundant use of `Value`. With `if` you can use `else if` so you don't actually need any nesting. – clcto Dec 14 '16 at 18:33
1

You should (IMHO) not use the conversion from boolean to int (or any other type). It relies on either tacit knowledge of the conversion rules and/or looking up the documentation.

Your requirement is :

Text is Value multiplied by 1.5 if Multiplier is true and Value staying the same if Multiplier is false, converted to a string.

So why not just code it this way, using a standard ternary operator:

Text = (Multiplier ? Value * 1.5 : Value).ToString(".0##");

Or some variant if you don't like the reference to Value duplicated.

It's clear what the two possible outcomes are, and does not rely on the rules converting a non-numeric type to a numeric type, and does not fall prey to the integer-division trap that your current code does.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Why he should not convert it from bool, https://msdn.microsoft.com/en-us/library/9y5sh618(v=vs.110).aspx doesn't say anything about it. – mybirthname Dec 14 '16 at 17:42
  • @mybirthname My point is it's not obvious that `true==1` and `false==0` unless you've been programming for a long time or remember the documented behavior. Plus you have to then apply that to the equation to understand what's _actually_ happening. Someone reading the code for the first time has to think about what it does (and might get it wrong). _Good_ code is easy to read, understand, and explain to others. – D Stanley Dec 14 '16 at 17:46
  • As a side note, I make the same argument when ordering by boolean values - I don't like having to think about which comes first so I make it explicit (e.g. `OrderBy(o => o.isValid ? 0 : 1)` versus `OrderBy(o => !o.isValid`) There's no _natural_ order to true and false - that's just the way Computer Science has defined it for years. – D Stanley Dec 14 '16 at 17:50
  • Okay I can agree than I never in my life wrote Convert.ToDouble(bool) and probably I will not, but in the end it is personal preference. Also I really don't like if statements or ? operators. But if I look into the problem on your side it is more clear to the reader what is going on with if/ ? operator. – mybirthname Dec 14 '16 at 17:56
  • Thanks, this is exactly what I wanted. A multiplier used wether the boolean is true or false. – Firro Dec 14 '16 at 18:06