N
N
Nikita Ignatenkov2015-09-02 13:05:43
Programming
Nikita Ignatenkov, 2015-09-02 13:05:43

Not correct conversion from float to int C#?

I can't figure out why it works differently. example below.
float val = 0.94;
(int)(val*100)
Result 93 ?
float val = 0.94;
float val1 = val*100;
(int)(val1)
Result 94 ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Kitaev, 2015-09-02
@kiting

Correct. Read about how floats work. Take a look at decimal. They may be suitable for your purposes.

M
Mrrl, 2015-09-02
@Mrl

Did not reproduce. Wrote like this:

static int Val1(){
            float val = 0.94f;
            return (int)(val*100);
        }
        static int Val2() {
            float val = 0.94f;
            float val1 = val*100;
            return (int)(val1);
        }

Both functions returned 94. How did you get 93?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question