# How to Calculate and Output Using Decimals

Page 1 of 1

## 7 Replies - 733 Views - Last Post: 22 February 2017 - 05:52 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=401605&amp;s=296bb239389abded7d7d5bd2703de3d7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hexagod

• 😂😂😂

Reputation: 31
• Posts: 577
• 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#

Is This A Good Question/Topic? 0

## Replies To: How to Calculate and Output Using Decimals

### #2 Skydiver

• Code herder

Reputation: 7053
• Posts: 23,973
• 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.

### #3 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6535
• Posts: 14,450
• 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.

### #4 hexagod

• 😂😂😂

Reputation: 31
• Posts: 577
• Joined: 29-October 16

## Re: How to Calculate and Output Using Decimals

Posted 22 February 2017 - 12:28 PM

I'm sorry

### #5 hexagod

• 😂😂😂

Reputation: 31
• Posts: 577
• 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

### #6 hexagod

• 😂😂😂

Reputation: 31
• Posts: 577
• 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...

I should have been using...

output.ToString();

Works now!!!! />

Skydiver, 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?

### #7 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6535
• Posts: 14,450
• Joined: 02-June 10

## Re: How to Calculate and Output Using Decimals

Posted 22 February 2017 - 04:51 PM

Congrats!

### #8 hexagod

• 😂😂😂

Reputation: 31
• Posts: 577
• 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.