NexusFi: Find Your Edge


Home Menu

 





Truncate a double, no rounding


Discussion in EasyLanguage Programming

Updated
    1. trending_up 405 views
    2. thumb_up 4 thanks given
    3. group 3 followers
    1. forum 4 posts
    2. attach_file 0 attachments




 
Search this Thread

Truncate a double, no rounding

  #1 (permalink)
jb2080
San Diego, California
 
Posts: 8 since Jun 2021
Thanks Given: 10
Thanks Received: 0

Hi everyone, I'm having trouble limiting a double to two decimal places but without rounding it. For example, I'd like to turn this double 0.2298 into this double 0.22
I tried things like: StrToNum(NumToStr(0.2298,4)) but StrToNum won't let you specify how many digits.

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Cheap historycal L1 data for stocks
Stocks and ETFs
MC PL editor upgrade
MultiCharts
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
REcommedations for programming help
Sierra Chart
What broker to use for trading palladium futures
Commodities
 
  #2 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629

jb2080,

when you want to truncate a value basic math seems your best approach. Dividing the integer portion (using the reserved work IntPortion) of your value after you multiplied it with 100 by 100 will give you the result you are looking for.
 
Code
	valueToTruncate = 0.2298 ;
		
	valueToTruncate = IntPortion( valueToTruncate * 100 ) / 100 ;
It should be noted, that while this will remove the "98" from your value, it will not change the precision of decimal variables or the number of decimal places stored internally by the platform.

Regards,

ABCTG



jb2080 View Post
Hi everyone, I'm having trouble limiting a double to two decimal places but without rounding it. For example, I'd like to turn this double 0.2298 into this double 0.22
I tried things like: StrToNum(NumToStr(0.2298,4)) but StrToNum won't let you specify how many digits.


Follow me on Twitter Reply With Quote
  #3 (permalink)
jb2080
San Diego, California
 
Posts: 8 since Jun 2021
Thanks Given: 10
Thanks Received: 0


Yes!!!! Wow that works great, thanks ABCTG!

Reply With Quote
  #4 (permalink)
 
trendisyourfriend's Avatar
 trendisyourfriend 
Quebec Canada
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: AMP/CQG
Trading: ES, NQ, YM
Frequency: Daily
Duration: Minutes
Posts: 4,527 since Oct 2009
Thanks Given: 4,176
Thanks Received: 6,020


jb2080 View Post
Hi everyone, I'm having trouble limiting a double to two decimal places but without rounding it. For example, I'd like to turn this double 0.2298 into this double 0.22
I tried things like: StrToNum(NumToStr(0.2298,4)) but StrToNum won't let you specify how many digits.

number = 0.2298
rounded_number = math.floor(number * 100) / 100
print(rounded_number) # Output: 0.22

Reply With Quote
Thanked by:
  #5 (permalink)
jb2080
San Diego, California
 
Posts: 8 since Jun 2021
Thanks Given: 10
Thanks Received: 0

This works correctly for me also, thanks!

Reply With Quote




Last Updated on July 24, 2023


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts