0

I have a variable:

float number;

this is assigned from user input.

If the variable is assigned 5 I want to convert it to a decimal and get a result of 0.5.

Or the user might input 56 in which case i want it to be converted to 0.56.

Or 678 to 0.678. I need the number in hand to be converted as the second part of a decimal number.

Basically the value of the float has to become the value after the decimal point in the number.

I've tried turning it into string first but no luck. Any ideas?

Phil P.
  • 969
  • 1
  • 5
  • 15
Mylonas K.
  • 181
  • 1
  • 13
  • 2
    You mean you want to divide it by 10? I'm not really clear on what you mean. – juharr Dec 06 '17 at 20:35
  • 1
    whats wrong with float number = 0.5; ? – Steve Dec 06 '17 at 20:36
  • Possible duplicate of [Division in C# to get exact value](https://stackoverflow.com/questions/15400903/division-in-c-sharp-to-get-exact-value) – Murat Gündeş Dec 06 '17 at 20:38
  • It's not clear what you're trying to do. You could either be asking for the decimal part, dividing the number to get a decimal or turn a percentage into a decimal. Please clarify so others can help you better. – LordNeo Dec 06 '17 at 20:40
  • 1
    the user might even type 56 i want it to be converted to 0.56. I need the number in hand to be converted as the second part of a decimal number. Be a fraction of it. I'm so sorry for being unclear! – Mylonas K. Dec 06 '17 at 20:43
  • its more clear now there is a second example. so the imput will always be an integer and you want to transpose that to immediately to the right of the decimal point? – Martin Smith Dec 06 '17 at 20:50
  • 1
    no, either im still not getting my point through or i'm just bad at understanding. The user is going to input a value in variable = number (ie 4 or 56 or 678 etc *never a float value though*). I want that number to be converted in to the right part of the decimal point. so it's gonna be like = 0,4 or 0,56 or 0,678 it's always a fraction of 1. Thanks for all the responses btw! @MartinSmith exactly. – Mylonas K. Dec 06 '17 at 20:52
  • 2
    `string input = "545"; float number = float.Parse("0." + input);` – derloopkat Dec 06 '17 at 20:58
  • @derloopkat Worked like a charm! Thank you very much. – Mylonas K. Dec 06 '17 at 21:03
  • The solution by @derloopkat would cause issues. First of all with floats that do have decimals. Second of all, for very large floats. Test it on the following float: `float number = 561220115;` or with `string input = "0.4"`. And his output is as `float` and not `decimal` as you've requested in the answer... Check this fiddle instead, it does exactly what you've asked: https://dotnetfiddle.net/Y9pWGY Please note that if your "input" is a float you will always lose precision. I strongly recommend writing user input into either a string or a decimal. – Phil P. Dec 06 '17 at 21:26
  • 1
    @PhilP. thank you very much for your answer. I did check the fiddle and i'll keep it in mind in future projects. As i mentioned before user can NOT input any sort of decimal numbers, only integers. – Mylonas K. Dec 06 '17 at 21:32
  • This seems like a strange thing to want. What if you want something that converts to `0.023`? If the user enters `023`, then you'll just end up with `0.23` instead. Can you tell us what this is actually _for_? We may be able to suggest better ways to solve the underlying problem. – Mark Dickinson Dec 07 '17 at 15:14
  • @MarkDickinson thanks for your reply. You're actually right. It doesn't accept the value 023, rather it understands it as 23. This is a unity-c# project. There is let's just say something like a calculator. The buttons of operations,numbers and everything are objects players need to step on to be activated. Imagine a giant floor calculator. So when the player hits the "," button he needs to be able to insert the number after the ",". – Mylonas K. Dec 10 '17 at 05:31

0 Answers0