C# how to allow users to input decimals as an answer? -
new language , site. i'm still bit confused differences between decimal , boolean.
let's have line.
console.writeline("enter change.");
i'm not sure put after accepts decimals user input. i've seen decimal.parse seems common answer not sure on how works.
as little information provided here, i'm not sure how long question last. despite this, seems you're working on console app , looking console.read()
or console.readline()
here. these take user input string, i'd imagine currency prefer in number format. seeing it's cents, guess int people aren't spending fractions of cent. code below uses int32.tryparse()
method, returns bool value regarding ability parse string int, , takes in string parse , int use out parameter. have enclosed in do/while statement ensure user inputs usable data, , prompted again if not case, followed output of change provide feedback upon success.
edit: appears author looking decimal input. code below uses decimals instead of integers (i had assumed change in cents rather change money given post-transaction)
bool successfulparse = false; decimal change; { console.writeline("enter change:"); successfulparse = decimal.tryparse(console.readline(), out change); } while (!successfulparse); console.writeline($"change {change} cents.");
this information readily available elsewhere, and, in future, may valuable include more information regarding problem receive more applicable answers.
Comments
Post a Comment