how can a make the floating variable show just 4 digits i mean : 8.6540098 to just 8.654 i guess what i'm trying to say is "less decimals" :D

Recommended Answers

All 11 Replies

Use the proper format specifier in a printf statement:

printf("%.4f", myFloatVar);

This will display 4 positions past the decimal. See printf doc for more details.

sorry, i didn't specified. it is for a Label i want the label to print just 4 digits

You then use:

printf("%4.3f", myFloatVar);

But note that if the value in "myFloatVar" has 2 digits left of the decimal, you'll get those two, along with the 3 past the decimal.

You then use:

printf("%4.3f", myFloatVar);

But note that if the value in "myFloatVar" has 2 digits left of the decimal, you'll get those two, along with the 3 past the decimal.

In my c++ book it shows how to do it differently. I'll just post that up to so the person who asked can have more options in choosing what he wants to do.

I'll use an example to explain it.
Suppose you want to display num which stands for 4.91877 .
To display the number normally, you would just do:

cout << num;

To display it with only four decimals, you would do this:

cout << setprecision(4) << num;

To display it with only 3 decimals, you would do this:

cout << setprecision(3) << num;

Like this, you would display the number with 2 decimals and 1 decimal.

In my c++ book it shows how to do it differently. I'll just post that up to so the person who asked can have more options in choosing what he wants to do.

I'll use an example to explain it.
Suppose you want to display num which stands for 4.91877 .
To display the number normally, you would just do:

cout << num;

To display it with only four decimals, you would do this:

cout << setprecision(4) << num;

To display it with only 3 decimals, you would do this:

cout << setprecision(3) << num;

Like this, you would display the number with 2 decimals and 1 decimal.

To use the setprecsion(4), you have to include<iomanip>.
Another option is to use cout.precision. For example :

const float PI = 3.14159265;
cout << PI << endl; //show with default precision
cout.precision(4);
cout << PI << endl; //shows with 4 digit precision

hey thanks guys, but i meant for a "Label" on the standard bar. I set the Label->Caption=variable so i need that the label shows only (4) decimals of that variable so it wont go out of the window...

I'm still confused. Is this for a WinForms application? I'm not sure what label you are talking about that has a caption (and I'm only guessing about what you mean by standard bar).

This will work for a generic label:

double pi = 3.141592654;
label1->Text = String::Format("{0:F4}",pi);

yap it is for a winForm application so i used a label but it doesn't have Text on its properties it has Caption. this is s syntax
Label1->Caption = tot; i want to make "tot" four digits or set the label sizes to only show 4 digits

As long as caption takes a string^ the above should work. It must be some other kind of control since a label does not have a caption member. Is it some kind of datagrid?

yea kinda, but i believe change the variable decimals to 4 digits before showing it on the Label, how can i convert the variable to 4 decimals and then save it to another variable to be shown?

Just create an intermediate string variable:

String ^ temp = String::Format("{0:F4}",pi);
after this, temp = "3.1416" you can do whatever you want with it
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.