# Formating the numbers in TLegend

**URL:** <https://root-forum.cern.ch/t/formating-the-numbers-in-tlegend/56286>\
**Category:** Newbie\
**Created:** [September 6, 2023, 8:06am UTC](https://root-forum.cern.ch/t/formating-the-numbers-in-tlegend/56286 "2023-09-06T08:06:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Natilus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/7cd45c/32.png) [@Natilus](https://root-forum.cern.ch/u/Natilus)\
**Post date:** [September 6, 2023, 8:06am UTC](https://root-forum.cern.ch/t/formating-the-numbers-in-tlegend/56286/1 "2023-09-06T08:06:02Z")

</div>

Dear experts,  
I have a plot and I create a legend for it including some numbers (yields). the numbers are shown with 6 digits after the decimal point. I would like to format the numbers shown in my Legend to show one digit after decimal point.  
example: 152.600000 → 152.6

Thank you for the help

 ![test](https://root-forum.cern.ch/uploads/default/original/3X/f/f/ffc30db16bc410fe01745de2263ae30b12c08b87.png)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 6, 2023, 8:40am UTC](https://root-forum.cern.ch/t/formating-the-numbers-in-tlegend/56286/2 "2023-09-06T08:40:02Z")

</div>

Can you post the code creating the legend ?

---

<div class="post-metadata">

**Author:** ![Natilus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/7cd45c/32.png) [@Natilus](https://root-forum.cern.ch/u/Natilus)\
**Post date:** [September 6, 2023, 9:01am UTC](https://root-forum.cern.ch/t/formating-the-numbers-in-tlegend/56286/3 "2023-09-06T09:01:44Z")

</div>

Hi @couet  
Thanks for the quick response. Here is the code:

```auto
std::string BMassCTitle = "B-Mass Corresponding to Different B-Vertex Probabilities";
	TCanvas* BMass_canv = new TCanvas("BVertexProbability_BMass",BMassCTitle.c_str());
		BMass_canv->SetWindowSize(WW, WH);
		BMass_canv->SetGridx();
		BMass_canv->SetGridy();
		gStyle->SetOptStat(0);
		Functions[0]->SetTitle("B-Vertex Probabilities");
		Functions[0]->GetXaxis()->SetTitle("M_{B}(GeV)");
		Functions[0]->GetXaxis()->SetRangeUser(5.08, 5.48);
		Functions[0]->GetXaxis()->SetNdivisions(8);
		Functions[0]->GetYaxis()->SetRangeUser(0,(Functions[0]->GetMaximum()+0.1*(Functions[0]->GetMaximum())));
		Functions[0]->SetLineColor(1);
		Functions[0]->SetFillColor(1);
		Functions[0]->SetFillStyle(3001);
		Functions[0]->Draw("LF2");
		for (int i=1; i<(int)Functions.size(); i++) {
			int Color = i+1;
			if (Color==10)
				Color = 46;
			Functions[i]->SetLineColor(Color);
			Functions[i]->SetFillColor(Color);
			Functions[i]->SetFillStyle(3001);
			Functions[i]->Draw("LF2,SAME");
		}
	TLegend* BMass_l = new TLegend(0.75,0.60,0.95,0.85);
		BMass_l->SetTextSize(0.015);
		BMass_l->SetNColumns(3);
		BMass_l->SetHeader(BMassCTitle.c_str(),"C");
		for (int j=0; j<(int)Functions.size(); j++) {
			BMass_l->AddEntry(Functions[j],("VtxProb_{B} > "+to_string(BVtxStart+(j*BVtxStep))).c_str(),"l");
			BMass_l->AddEntry((TObject*)0,("SY = "+to_string(Yields[j].first.first)+" +/- "+to_string(Yields[j].first.second)).c_str(),"");
			BMass_l->AddEntry((TObject*)0,("BY = "+to_string(Yields[j].second.first)+" +/- "+to_string(Yields[j].second.second)).c_str(),"");
		}
		BMass_l->Draw();
	BMass_canv->Update();

```

In the above code, Functions is an array that contains TF1\* objects.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 6, 2023, 9:42am UTC](https://root-forum.cern.ch/t/formating-the-numbers-in-tlegend/56286/4 "2023-09-06T09:42:23Z")

</div>

in your example `to_string` does the conversion to string. I am not sure you can specify the formatting for `to_string`

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [September 6, 2023, 9:51am UTC](https://root-forum.cern.ch/t/formating-the-numbers-in-tlegend/56286/5 "2023-09-06T09:51:09Z")

</div>

Use: [c++ - Set precision of std::to\_string when converting floating point values - Stack Overflow](https://stackoverflow.com/a/16606128/7471760)  
or: [std::format in C++20 – MC++ BLOG](https://www.modernescpp.com/index.php/std-format-in-c-20/) if you can use C++20

---

<div class="post-metadata">

**Author:** ![Natilus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/7cd45c/32.png) [@Natilus](https://root-forum.cern.ch/u/Natilus)\
**Post date:** [September 6, 2023, 9:56am UTC](https://root-forum.cern.ch/t/formating-the-numbers-in-tlegend/56286/6 "2023-09-06T09:56:40Z")

</div>

The reason I used to\_string() is because I thought I can only use strings in the AddEntry method.  
Is it possible to format before turning into string? or just not use strings?

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [September 6, 2023, 10:14am UTC](https://root-forum.cern.ch/t/formating-the-numbers-in-tlegend/56286/7 "2023-09-06T10:14:36Z")

</div>

Try what ferhue suggested, or you can also use Form; e.g. if `val` is a double and you want 1 decimal:

```auto
BMass_l->AddEntry(mygraph,Form("%.1f",val),"l");

```

---

<div class="post-metadata">

**Author:** ![Natilus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/7cd45c/32.png) [@Natilus](https://root-forum.cern.ch/u/Natilus)\
**Post date:** [September 6, 2023, 10:34am UTC](https://root-forum.cern.ch/t/formating-the-numbers-in-tlegend/56286/8 "2023-09-06T10:34:46Z")

</div>

Hi @dastudillo,  
That worked perfectly!  
Thank you all for your help.
