Convert.ToDouble Method

Hello guys,

I got a question that converting string data type to double.
For instance, I want to get maintain decimal 2 places of 573 so I did

string a = Math.Round(573, 2).ToString(“F2”);

However, I need the data type as “double”, so I used “Convert.ToDouble(a)” method .

double b = Convert.ToDouble(a);

It turns out that it removes the decimals 2 places that I want to keep.
I 'd like to knows how to maintain the decimal places and return a double data type.

Thank you, guys!

Decimal.gh (3.0 KB)

double itself doesn’t store how many digits are shown after the decimal separator.

or if there is a way to maintain the decimals?

If you call:

RhinoApp.WriteLine(a);

it returns 573.00.

GH panels (GH string format) eliminate unnecessary zeros I guess, but they’re there.

It dose’t remove the zeros, but i think the Convert.ToDouble method does…

Ups sorry, I thought the variable a came out of output A and not B. Let me see it again.

Not sure why you do so difficult conversion between different types:

double c = Math.Round(x, 2);
string b = string.Format("{0:0.00}", x);

A = c;
B = b;

Hep, that was supposed to be reply to OP @wacdemic0206

1 Like

Wait, this doesn’t make sense. The number is the number, no matter how many zeros it has. What you’re interested in is how you convert it to text for display, what’s the problem you have? You have ways to print it with 2 decimals.

Hello, thanks for your reply
The reason why I did that because the result I need must have two decimals and it has to be a “double” data type.

Or if there is a better way to achieve this please let me know.

Thank you so much

Hello,

I use a function that returns a double, and it sometimes doesn’t have decimals.
The problem I have is that I need the result that always keeps 2 (or flexible) decimal places and it has to be a “double” data type.

For doubles => Math.Round(x, 2);
For strings => x.ToString(“N2”);

You know this, but you still see a problem, so something else is wrong.

1 Like