String returned from wxString::Format("%f", d) too short Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
The20
Earned a small fee
Earned a small fee
Posts: 13
Joined: Mon Sep 12, 2011 11:02 pm

String returned from wxString::Format("%f", d) too short

Post by The20 »

Hello.
When i convert a double into a wxString using wxString::Format("%f", double) and put the result in a wxGrid using wxGrid::SetCellValue(row, col, wxString::Format("%f", double)) the resulting string always has only 6 characters after the decimal digit, which means important information are missing (I have double values that are 1e-7 or 1e-8. That 0.00000001 is important!).
I tried to set the cell formating using wxGrid::SetColFormatFloat(i, -1, 9) but the result is the same (for some reason it did work when i set it to 10, though), but even if that worked i would have to make assumptions about the data someone using this program may be working with in 2 years AND i always have many digits behind the decimal point, even when working with numbers that are integers. Both things are not good.

So, first of, where are the numbers cut and how do i prevent that?
And second, is there a way to display really long numbers in a wxGrid without having to resort to fix the number of decimal places being displayed?
asadilan
Earned some good credits
Earned some good credits
Posts: 147
Joined: Tue Jul 27, 2010 10:42 pm

Re: String returned from wxString::Format("%f", d) too short

Post by asadilan »

this does not answer your question. but you could always use a big integer.
so for 0.00000000000001, it can be 1 and for 1 it can be 10000000000 of course assuming that the number does not exceed the variable type u r using.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: String returned from wxString::Format("%f", d) too short

Post by doublemax »

6 digits is the default precision for %f if you don't explicitly set it otherwise.
http://support.microsoft.com/kb/29557

I assume this article refers to Visual C++ compiler, i don't know about the standard libraries on other platforms/compiler.
Use the source, Luke!
RainRat
I live to help wx-kind
I live to help wx-kind
Posts: 178
Joined: Thu Jan 06, 2011 11:26 pm

Re: String returned from wxString::Format("%f", d) too short

Post by RainRat »

The actual code you could use is

Code: Select all

  wxString::Format("%.9f", double); // 9 decimal places
  wxString::Format("%.15f", double); // 15 decimal places
  wxString::Format("%.9e", double); // scientific notation with 9 decimal places
The20
Earned a small fee
Earned a small fee
Posts: 13
Joined: Mon Sep 12, 2011 11:02 pm

Re: String returned from wxString::Format("%f", d) too short

Post by The20 »

doublemax wrote:6 digits is the default precision for %f if you don't explicitly set it otherwise.
http://support.microsoft.com/kb/29557

I assume this article refers to Visual C++ compiler, i don't know about the standard libraries on other platforms/compiler.
I'm using VC++ so it applies anyways. Guess i should have mentioned that in the OP.
RainRat wrote:The actual code you could use is

Code: Select all

  wxString::Format("%.9f", double); // 9 decimal places
  wxString::Format("%.15f", double); // 15 decimal places
  wxString::Format("%.9e", double); // scientific notation with 9 decimal places
I was hoping there would be a way around that. I guess i could write a method that cuts off unnecessary zeros, but still ...
amk_tt
Earned a small fee
Earned a small fee
Posts: 19
Joined: Sat Nov 28, 2009 4:45 pm
Location: Russia

Re: String returned from wxString::Format("%f", d) too short

Post by amk_tt »

Also

Code: Select all

wxString::Format("%.9g", d); // scientific or floating point (what shorter) notation with 9 decimal places
Post Reply