简体   繁体   中英

How to add number below 1 (decimal) onto the end of a number in c#

Hi sorry for the bad title, but this has got me stumped and I need some help. I am making a calculator program in C# that has a GUI. In order to use the GUI I need to make it so that a number is created by clicking numbers one at a time (If this doesn't make any sense here is an example: Person presses 1 then 2 the calculator displays 12. The way I achieved this is by having var[varIndex] = (var[varIndex] * 10) + 1 if the key being pressed is 1 where var is an array of the totals entered.

So what I need to do now is append a decimal onto the end so what I have done so far is set a boolean that will be true if the decimal button is selected switching all the number buttons mode to decimal, so now I need a formula that will add a number onto the end past a decimal (for example: Person presses 1 then . then 2 then 5 they will get 1.25) I can't find a formula for this though!

So my question is what formula will append a number on the end for a decimal.

Will i have to gut my program and make it take in text then convert to the numbers? Or is there a formula? Thanks a ton and sorry for the terrible explanation.

As Jonesy (and others) mentioned in the comments, this would be a lot easier if you stored the current number as a string and appended characters to it, and then use Double.TryParse(..) to turn it into a number.

However, if you're determined to treat it as a number, you can simply do something along the following lines:

var[varIndex] += enteredNumber / (10 * numEntriesSinceDecimal);

numEntriesSinceDecimal would start at 0, and increment with each button the user pressed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM