Skip to content

Quick-tip 28 | Determining the number of decimal places for a text label

You may need to add a text label to a chart which displays price information. If you are using the Text_New syntax you will need to convert the number into a string (using the NumToStr syntax). To do this you need to define the number of decimal places. You could hard code this or add an input to your program, but I prefer to use the following code so that the number of decimal places is determined programatically.

This may not work for all price eventualities, but it works for most of the situations I have encountered.

Vars: string Txt( "" ), // Used in the determination of the number of decimal places required for text drawing
Intrabarpersist int DecPlaces( 5 ); // Stores the number of decimal places to use to draw text values

Once
Begin
// Determine the number of decimal places required for text drawing
Txt = NumToStr( FracPortion( MinMove / PriceScale ), 5 );
If RightStr( Txt, 1 ) <> "0" then DecPlaces = 5;
If RightStr( Txt, 2 ) = "00" then DecPlaces = 3;
If RightStr( Txt, 3 ) = "000" then DecPlaces = 2;
If RightStr( Txt, 4 ) = "0000" then DecPlaces = 1;
If RightStr( Txt, 5 ) = "00000" then DecPlaces = 0;
End;