Main Site Documentation

Convert double to int


#1
using Gadgeteer.Modules.GHIElectronics;

namespace ledcontroller2
{
    public partial class Program
    {
        int a;
        int b = 1000;
        void ProgramStarted()
        {
            Debug.Print("Program Started");
            double x = joystick.GetJoystickPosition().X;
            Debug.Print("X = " + x);
            if (x > 0.05)
            Convert.ToInt32(x);
            {
             a = x * b;
            }
            led7r.Animate(a, true, true, false);
        }
    }
}

Somehow [quote]a = x * b;[/quote] just wont work, i also tried to convert x ( [quote]Convert.ToInt32(x);[/quote])
Maybe the mistake is obvious and stupid, but i would be glad if u could help me :slight_smile:


#2

@ andre.m -
i already tried that
Doesn’t work for me either …


#3

That’s because you already declared “a” as an int and then tried to do it again.


#4
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace ledcontroller2
{
    public partial class Program
    {
        int b = 1000;
        void ProgramStarted()
        {
            Debug.Print("Program Started");
            double x = joystick.GetJoystickPosition().X;
            Debug.Print("X = " + x);
            int a = (int)x;
            if (x > 0.05)
            {
                a = x * b;
                led7r.Animate(a, true, true, false);
            }
        }
    }
}

Still not working


#5

Thats why I tought I’d try to convert x to int.
You gotta know that im very new into c# and programming in general :wink:
Using it for not quite 3 days now.


#6

Or to keep the precision of x in multiplication you could do

a = (int)(x * b);