Double.ToString() Not working correctly - General Discussion - Netduino Forums
   
Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Double.ToString() Not working correctly


  • Please log in to reply
6 replies to this topic

#1 gismo

gismo

    Advanced Member

  • Members
  • PipPipPip
  • 110 posts

Posted 12 September 2015 - 01:01 PM

I'm trying to show two decimal places and using the limited format specifiers it's not working. I'm trying this loop and I get the same result: 22.5034567...

 

 

            double myDouble = 22.503456785478;
            string[] formats = new string[]{"","f2","g2","n2", "d2", "0.00","N2"};
            
            for (int i  = 0;i<formats.Length;i++)
            {
                LCD.Clear();
                LCD.Write(formats[i] + ": " + myDouble.ToString()); //22.50 expected result
                Thread.Sleep(1000);
            }
Aside from writing a custom function to do this, is this a bug? Anyone else experience this? N2 netmf4.3


#2 TechnoGuy

TechnoGuy

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts
  • LocationCanada

Posted 12 September 2015 - 03:42 PM

Try using this instead:  myDouble.ToString("F")

 

e.g.

  • LCD.Write(myDouble.ToString("F"));
  • Debug.Print(myDouble.ToString("F"));

 

See this MSDN Article:

Standard Numeric Format Strings

https://msdn.microso...(v=vs.110).aspx


- Ian

 

My Current Dev Boards:

  • 3 x Netduino Plus 2
  • 1 x Netduino 3 WiFi

#3 gismo

gismo

    Advanced Member

  • Members
  • PipPipPip
  • 110 posts

Posted 12 September 2015 - 05:23 PM

Try using this instead:  myDouble.ToString("F")

 

e.g.

  • LCD.Write(myDouble.ToString("F"));
  • Debug.Print(myDouble.ToString("F"));

 

See this MSDN Article:

Standard Numeric Format Strings

https://msdn.microso...(v=vs.110).aspx

 

No luck. Same result...I switched back to my GHI Cerb device and parameters F2, N2, G2 etc all work. 



#4 JoopC

JoopC

    Advanced Member

  • Members
  • PipPipPip
  • 148 posts

Posted 12 September 2015 - 06:09 PM

No it works correct:

Dim myDouble As Double = 22.503456785478

Debug.Print(myDouble.ToString("F2"))

result "22.50"

#5 gismo

gismo

    Advanced Member

  • Members
  • PipPipPip
  • 110 posts

Posted 12 September 2015 - 07:07 PM

@JoopC I wish it were true....for my device. Are you testing on a netduino 2??



#6 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 12 September 2015 - 07:09 PM

Have you tried

LCD.Write(formats[i] + ": " + myDouble.ToString(formats[i]));

With the code as stated I'd expect the ToString to return the same result every time.

 

Regards,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#7 KiwiDev

KiwiDev

    Advanced Member

  • Members
  • PipPipPip
  • 100 posts
  • LocationNew Zealand

Posted 13 September 2015 - 12:16 AM

Hi Gismo,

 

Just to double check I ran this on my N2P running NetMF 4.3, 1

 

I replaced LCD.Write with Debug.Print

 

@KiwiBryn

blog.devmobile.co.nz

 

public static void Main()
{
   double myDouble = 22.503456785478;
   string[] formats = new string[] { "", "f2", "g2", "n2", "d2", "0.00", "N2" };
 
   for (int i = 0; i < formats.Length; i++)
   {
       Debug.Print(formats[i] + ": " + myDouble.ToString()); //22.50 expected result
       Thread.Sleep(1000);
   }
}
 
Displayed ( as expected as format string not specified)
 
: 22.503456785478001
f2: 22.503456785478001
g2: 22.503456785478001
n2: 22.503456785478001
d2: 22.503456785478001
0.00: 22.503456785478001
N2: 22.503456785478001
The thread '<No Name>' (0x1) has exited with code 0 (0x0).
 
 
Then "fixed" version  (I had to remove 0.00 as it was unsupported)
 
public static void Main()
{
   double myDouble = 22.503456785478;
   string[] formats = new string[] { "", "f2", "g2", "n2", "d2", "N2" };
 
   for (int i = 0; i < formats.Length; i++)
   {
      Debug.Print(formats[i] + ": " + myDouble.ToString(formats[i])); 
      Thread.Sleep(1000);
   }
}
 
DIsplayed 
: 22.503456785478001
f2: 22.50
g2: 22.503456785478001
n2: 22.50
d2: 22.50
N2: 22.50
The thread '<No Name>' (0x1) has exited with code 0 (0x0).





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.