7 Replies - 580 Views - Last Post: 22 February 2017 - 05:52 PM Rate Topic: -----

#1 hexagod  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 263
  • Joined: 29-October 16

How to Calculate and Output Using Decimals

Posted 21 February 2017 - 05:10 PM

Hay Guyz,
So I'm just about wrapping up this Android Subwoofer Resistance calculator project. The project is just about done but I've got one major problem left:

when my calculations involve and output that is a whole number I have no problem with the output... but when the calculation involves a decimal output IE: .5 it's output as 0.

How can I fix this?

This isn't the exact code that I'm using but It's illustrative of how I'm coding this.

double subs;
double coilspersub;
double ohmspercoil;
double ohmspersub;
double output;

[Insert generic android XML associations]

ohmspercoil = Convert.ToDouble(ohmsPerCoilField.Text);
subs = Convert.ToDouble(subsField.Text);
coilspersub = Convert.ToDouble(coilsPerSub.Text);

// series

ohmspersub = ohmspercoil * coilspersub;

//parallel

output = ohmspersub / subs;

outputField.Text = Convert.ToString(output) + " Ω";




When that result is a whole it works fine but when it's .5 it shows as 0.... I'm in Xamarin / VS 2015 compiling for Droid in C#

Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: How to Calculate and Output Using Decimals

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6164
  • View blog
  • Posts: 21,248
  • Joined: 05-May 12

Re: How to Calculate and Output Using Decimals

Posted 21 February 2017 - 06:27 PM

Why not simply use Double.ToString()? If needed, use the variant that takes format specifiers if you need your double to be displayed a particular way.
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6530
  • View blog
  • Posts: 14,446
  • Joined: 02-June 10

Re: How to Calculate and Output Using Decimals

Posted 22 February 2017 - 01:29 AM

A few observations:

One: Glad to see you jumping into Xamarin
Two: Just out of personal curiosity...
If you're using Xamarin, why are you specifically building/targeting just Xamarin.Android?
Why wouldn't you choose to go Xamarin.Forms so your app can deploy to Android, iOS, UWP?
I'm just wondering if there is something you're gaining by taking this path.

Three: ohmspercoil = Convert.ToDouble(ohmsPerCoilField.Text);
You're not really reaching into the UI and grabbing an entry box directly then grabbing its .Text like it was 1998 WinForms, are you?
Please please please... Stop this craziness ASAP. If you're going to move forward from WinForms to Xamarin don't take all the bad with you. Moving to a XAML/MVVM application eco-system is more than just how you make the UI. Its a whole new paradigm. Please for the love of code read up on MVVM design pattern and take it to heart. There are a ton of really good articles/tutorials out there. For now this should give you a decent overview.
https://redpillxamar...evisiting-mvvm/


Four: This isn't the exact code that I'm using
Normally the response would be "Then we can't help you". Because all too often the example is different in some way from the real code. For example, here everything is a double. I'd bet that someplace in your real code you have an int and so your calculations are being implicitly cast to an int along the way. Thus 0.5 becomes 0.
Was This Post Helpful? 1
  • +
  • -

#4 hexagod  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 263
  • Joined: 29-October 16

Re: How to Calculate and Output Using Decimals

Posted 22 February 2017 - 12:28 PM

I'm sorry :( :stupid:
Was This Post Helpful? 0
  • +
  • -

#5 hexagod  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 263
  • Joined: 29-October 16

Re: How to Calculate and Output Using Decimals

Posted 22 February 2017 - 03:53 PM

thanks for the help guys! It's been a crazy day so haven't had a chance to try those solutions but will in a bit here. Appreciate it
Was This Post Helpful? 0
  • +
  • -

#6 hexagod  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 263
  • Joined: 29-October 16

Re: How to Calculate and Output Using Decimals

Posted 22 February 2017 - 04:28 PM

w0000000000000000000000000000000ttttttt just figured it out.


OK so problem was...

instead of using Convert.ToString(output);

I should have been using...

output.ToString();

Works now!!!! :bananaman:/>

Attached Image

View PostSkydiver, on 21 February 2017 - 06:27 PM, said:

Why not simply use Double.ToString()? If needed, use the variant that takes format specifiers if you need your double to be displayed a particular way.


I did try that and it said I couldn't take a double and turn it into a string for some reason. Maybe I was missing an inclusion?
Was This Post Helpful? 1
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6530
  • View blog
  • Posts: 14,446
  • Joined: 02-June 10

Re: How to Calculate and Output Using Decimals

Posted 22 February 2017 - 04:51 PM

Congrats!
Was This Post Helpful? 0
  • +
  • -

#8 hexagod  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 263
  • Joined: 29-October 16

Re: How to Calculate and Output Using Decimals

Posted 22 February 2017 - 05:52 PM

ty for the advice man. don't think I'm not listening but I'm very limited on time so I have to be as pragmatic as I can be. I'm definitely going to look at your tutorials eventually but right now it's all about pragmatism.

Since I am teaching myself how to program I try and adapt lessons from other people which means I don't always do things the right way. I apologize for the crazyness! .. Noone ever taught me how to program I'm just learning as I go.

I do appreciate the help quite a bit.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1