Is it possible to get an "exact" value? - page 2

 

but this value

0.099999999

will return 0.1

)

 

then what was quoted above would be more appropriate to the task of the TC:

void OnStart()
  {
//---
   int DIGITS=2;
   string stroka=DoubleToString(0.099999999,8);
   int P=StringFind(stroka,".");
   Print(stroka);
   stroka=StringSubstr(stroka,0,P+DIGITS+1);
   Print(StringToDouble(stroka));
   Print(NL(DIGITS,0.099999999));
   
  }
//+------------------------------------------------------------------+
double NL (int DIGITS,double value) {
   double step=1/MathPow(10,DIGITS);
   return (MathFloor(value/step)*step);
}

Returns:

2014.10.04 11:42:31.856 normalize EURUSD,H4: 0.09
2014.10.04 11:42:31.856 normalize EURUSD,H4: 0.1
2014.10.04 11:42:31.856 normalize EURUSD,H4: 0.10000000
 

I sketched out a function that does exactly what I want it to do: precisely truncates a string to the specified precision , unlike DoubleToStr:

string Normalize (string Normalize_Parse, int Normalize_Number, string Normalize_Separator = ".")
{
   string Normalize_Before,
          Normalize_After;
   
   for (int i = 0; i <= StringLen (Normalize_Parse) - 1; i ++)
   {
      if (StringSubstr (Normalize_Parse, i, StringLen (Normalize_Separator)) == Normalize_Separator)
      {
         for (int ii = i + StringLen (Normalize_Separator); ii < i + StringLen (Normalize_Separator) + Normalize_Number; ii ++)
         {
            Normalize_After += StringSubstr (Normalize_Parse, ii, 1);
         }
         
         break;
      }
      
      Normalize_Before += StringSubstr (Normalize_Parse, i, 1);
   }
   
   return (Normalize_Before + (Normalize_After > 0 ? Normalize_Separator + Normalize_After : ""));
}

Result for "0.09864724":

Comment (Normalize (0.09864724, 2)); // 0.09

Result for"0,-843158":

Comment (Normalize ("0,-843158", 3, ",-")); // 0,-843

Operations with double and similar:

Comment ((double) Normalize (0.09, 2) * 5); // 0.45

I've been searching through and such a function already exists in MQL4 (5?). If it's not there, why not add it a long time ago? :)

 
WePlexus:

I sketched out a function that does exactly what I want it to do: precisely truncates a string to the specified precision , unlike DoubleToStr:

Result for "0.09864724":

Result for"0,-843158":

Operations with double and similar:

I've been searching through and such a function already exists in MQL4 (5?). If it's not there, why not add it a long time ago? :)

that's what I call "easier" )
 
WePlexus:

I sketched out a function that does exactly what I want it to do: precisely truncates a string to the specified precision , unlike DoubleToStr:

Result for "0.09864724":

Result for"0,-843158":

Operations with double and similar:

I've been searching through and such a function already exists in MQL4 (5?). If it's not there, why not add it a long time ago? :)

You're operating with some strange examples - that's why it's impossible to conclude that your function works correctly.

take this example:

how to leave =0.01999999999999999999 -- leave =0.01
 
abolk:
You're operating with out-of-character examples -- so you can't conclude that your function works correctly.

take this example:

how to leave =0.01999999999999999999 -- leave =0.01
checked the last function, it leaves 0.01.
 

I still don't understand what you want.

If you want to see 0.098 on the chart instead of 0.098, then do so!

int a=0.098 * 100;

double d=a/100.0;

Comment(StringFormat("%G", d );

 
sanyooooook:
checked the last function, leaves 0.01
Checked your function -- leaves 0.02
 
abolk:
Checked your function -- leaves 0.02.

Indeed, 0.02.

My function may be more cumbersome, but it cuts off exactly 0.01.

 
sanyooooook:
checked the last function, leaves 0.01
How can your function be optimised to take 0.01 from theabolk example?
Reason: