How to cut the last decimals?

 

Hi,

I use an alert-message containing Close[0] and the price shown in the alert window is displayed like this: 1.34420000 (EURUSD) or 134.25000000 (EURJPY).

Can someone please tell me how to cut the last zeros? I didn't find anything with the search function.

Thank you!

 
mar:

Hi,

I use an alert-message containing Close[0] and the price shown in the alert window is displayed like this: 1.34420000 or 134.25000000.

Can someone please tell me how to cut the last zeros? I didn't find anything with the search function.

Thank you!

You are using + to concatenate the strings, in addition use DoubleToStr() and specify the number of digits you want.
 
Great, it works. Thank you!!
 
mar: displayed like this: 1.34420000 (EURUSD) or 134.25000000 (EURJPY). Can someone please tell me how to cut the last zeros?
  1. You are using
    Alert( "string"+Close[0] );
    If you use separate arguments you'll get only 4 digits (1.3442 1.34.2500)
    Alert( "string", Close[0] );
    Instead, format the number to correct number of digits (1.34420 or 134.250 on a 5 digit broker)
    string   PriceToStr(double p){   return( DoubleToStr(p, Digits) );            }
    :
       Alert( "string"+PriceToStr(Close[0]) );

  2. By your post, you really don't want to cut ALL the "last zeros" (1.35000 -> 1.35) but should you really mean that: Alert( "string", DoubleToString(Close[0]) );
    string   DoubleToString(double v, int d=8){              // 1.60000000 -> 1.6
       string   value = DoubleToStr(v, d);
       for(int iRight = StringLen(value)-1; true; iRight--){
          int      c = StringGetChar(value, iRight);
          if(c != '0'){
             if(c == '.') iRight--;
             break;
          }
       }
       return( StringSubstr(value, 0, iRight+1) );
    }
    string   SDoubleToStr(double v, int d, string s="+"){       if (v < 0.) s = "";
                                                 return( s + DoubleToStr(v, d) ); }
    string   SDoubleToString(double v, string s="+", int d=8){  if (v < 0.) s = "";
                                              return( s + DoubleToString(v, d) ); }
    

Reason: