Jump to content
  • Advertisement

Recommended Posts

Hi

heading is float alike 12.3524423

std::string sheading = std::to_string(heading);

now I only want to display 12.35

Many thanks in advance

 

Share this post


Link to post
Share on other sites
Advertisement

Or, if you don't mind using Boost, this:

	#include <iostream>
	#include <boost/format.hpp>
	#include <string>
	 
	int main()
	{
	  double x = 12.3524423;
	  std::string s = boost::str(boost::format("%.2f") % x);
	  std::cout << s << '\n';
	}

Share this post


Link to post
Share on other sites
7 hours ago, mike44 said:

Simple as:

sprintf(headingn,"%1.2f", heading);

That's simple, but also unsafe. How large is the buffer `headingn'? At the very least, you should use snprintf instead.

If you can avoid using C-style string manipulation, you are much less likely to introduce a buffer overflow in your program.

 

Share this post


Link to post
Share on other sites

Better thing is to cut it like that that should work

 

template <class type> type  TruncX(type x, int len)
{
	type denom = type(10.0);
	for (int i=0; i < len; i++)
		denom = denom * type(10.0);
return type(int(x*denom) / denom);
}

 

Share this post


Link to post
Share on other sites

Ok then ;p

inline AnsiString truncfstr(AnsiString text, AnsiString sign, int precision)
{
AnsiString s = text;
AnsiString tmp;
		tmp = stddelete(s,Pos(sign,s)+sign.length()+precision, 10000000);


return tmp;
}


inline int Pos(AnsiString sub, AnsiString str)
{
	 std::size_t found = str.find(sub,0);
	  if (found!=AnsiString ::npos)
		  return int(found)+1;
	  else
		  return 0;
}


inline AnsiString stddelete(AnsiString str, int pos, int len) //this is for AnsiString  only because i will call only Pos()-1 from it
{
	AnsiString s = str;
	 s.erase(pos, len);
	 return s;
}

Where typedef std::string AnsiString;

Should work

Share this post


Link to post
Share on other sites

Truncation like that is one way to do it, but most applications don't want that.

For an example, a value of 123.000001 that has a floating point rounding error beyond the value would be truncated to 123.00.  However, a value of 122.9999999 that has a rounding error below the intended value would unexpectedly go to 122.99 instead of the correct 123.00.

There are several different rounding modes. The ones typically used in computers (and supported by hardware) are:

  • Round to nearest, ties to even. This is also called "banker's rounding". 
  • Round to nearest, ties away from zero.
  • Round up
  • Round down
  • Round to zero, aka truncation.  This is the one you're doing.

The default mode -- which is also the version humans typically expect -- is the first.  

The programming language provides string manipulation functions that correctly round the value.  The printf family, the stream family precision functions, and Boost's string manipulation families all have the code to correctly round the result.  The first two are part of the language directly, there is no extra cost to use them. The Boost version is an extremely common library I've had available in most professional projects. They are already present and included in the code. Use the tools that are available to you.

Share this post


Link to post
Share on other sites
On 1/12/2018 at 10:30 PM, alvaro said:

Or, if you don't mind using Boost, this:

 

On this topic, Boost.Format is a header only library, so you don't need to add any libraries, dlls, etc to your project. 

Just note that Boost.Format is significantly slower than printf. That's not necessarily an issue (most applications don't spend all their time formatting strings), but just be aware of it.

Depending on your platform, you could also use Microsofts "secure" versions of sprintf

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!