From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Capacitance Conversion

Solved!
Go to solution

How do I convert a Capacitance reading into the reading type?

 

What I mean by that is, say I get a reading like this 0.000000893544296332038

How do I convert it so that i get it as nF or uF etc.

 

 

I imagine its something to do with the range?

0 Kudos
Message 1 of 3
(2,051 Views)
Solution
Accepted by topic author Gazlar

Hi,

Here's some code I found a while ago that works very well.

 

private string ToEngineeringNotation(double d)
        {
            double exponent = Math.Log10(Math.Abs(d));
            if (Math.Abs(d) >= 1)
            {
                switch ((int)Math.Floor(exponent))
                {
                    case 0:
                    case 1:
                    case 2:
                        return d.ToString();
                    case 3:
                    case 4:
                    case 5:
                        return (d / 1e3).ToString() + "k";
                    case 6:
                    case 7:
                    case 8:
                        return (d / 1e6).ToString() + "M";
                    case 9:
                    case 10:
                    case 11:
                        return (d / 1e9).ToString() + "G";
                    case 12:
                    case 13:
                    case 14:
                        return (d / 1e12).ToString() + "T";
                    case 15:
                    case 16:
                    case 17:
                        return (d / 1e15).ToString() + "P";
                    case 18:
                    case 19:
                    case 20:
                        return (d / 1e18).ToString() + "E";
                    case 21:
                    case 22:
                    case 23:
                        return (d / 1e21).ToString() + "Z";
                    default:
                        return (d / 1e24).ToString() + "Y";
                }
            }
            else if (Math.Abs(d) > 0)
            {
                switch ((int)Math.Floor(exponent))
                {
                    case -1:
                    case -2:
                    case -3:
                        return (d * 1e3).ToString() + "m";
                    case -4:
                    case -5:
                    case -6:
                        return (d * 1e6).ToString() + "μ";
                    case -7:
                    case -8:
                    case -9:
                        return (d * 1e9).ToString() + "n";
                    case -10:
                    case -11:
                    case -12:
                        return (d * 1e12).ToString() + "p";
                    case -13:
                    case -14:
                    case -15:
                        return (d * 1e15).ToString() + "f";
                    case -16:
                    case -17:
                    case -18:
                        return (d * 1e15).ToString() + "a";
                    case -19:
                    case -20:
                    case -21:
                        return (d * 1e15).ToString() + "z";
                    default:
                        return (d * 1e15).ToString() + "y";
                }
            }
            else
            {
                return "0";
            }


        }

 

Curt

Message 2 of 3
(2,029 Views)

Thanks for that! Smiley Happy

0 Kudos
Message 3 of 3
(1,995 Views)