Quantcast
Channel: Rainmeter Forums
Viewing all articles
Browse latest Browse all 1572

Feeds & Internet Data • Re: Market Prices

$
0
0
I would like to display the prices with only one decimal instead of two.
Apologies for the tardy reply. Here is one way to show variable numbers of decimal places for the prices.

I'll just show you how to do it for the first index or stock and you can make the chamges for your other stocks.

1) Firstly add the number of decimal places you want in the [Variables] section of MarketPrices.ini.

Code:

[Variables]DecPlaces1=2
2) Add the following underneath [mAddStocksScript] in MarketPrices.ini

Code:

[FormatPriceScript]Measure=ScriptScriptFile=FormatPrice.luaUpdateDivider=-1
3) Find [mIndex1_Price] and replace it with the following

Code:

[mIndex1_Price]Measure=WebParserUrl=[InfoIndex1]StringIndex=2;;>>-- add this line to take comas out of the price stringSubstitute=",":""
4) Add the following calc measure underneath [mIndex1_Price] to convert the string price to a number

Code:

[cIndex1_PriceCalc]Measure=CalcFormula=[mIndex1_Price]*1
5) Find [LabelPrice1] and replace it with the following

Code:

[LabelPrice1]Meter=STRINGMeterStyle= sTextRight | sColorSetGrayx=#Col2XPos#Text=[&FormatPriceScript:format_price( [&cIndex1_PriceCalc] ,  #DecPlaces1#  )]
6) Copy the following and save it as FormatPrice.lua in the same directory as MarketPrices.ini

Code:

-- FormatPrice.lua-- adapted from http://lua-users.org/wiki/FormattingNumbers-- add commas to separate thousandsfunction comma_value(amount)  local formatted = amount  while true do      formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')    if (k==0) then      break    end  end  return formattedend-- round number to nearest decimal placesfunction round(val, decPlaces)  if (decPlaces) then    return math.floor( (val * 10^decPlaces) + 0.5) / (10^decPlaces)  else    return math.floor(val+0.5)  endend--  ------------------------------------------------------------- format_price formats output with comma to separate thousands-- and rounded to given decimal placesfunction format_price(amount, decPlaces )  local str_amount,  formatted, famount, remain  decPlaces = decPlaces or 2  -- default 2 decPlaces places    famount = math.abs(round(amount,decPlaces))  famount = math.floor(famount)  remain = round(math.abs(amount) - famount, decPlaces)-- add commas to separate the thousands  formatted = comma_value(famount)-- attach the decimal places portion  if (decPlaces > 0) then    remain = string.sub(tostring(remain),3)    formatted = formatted .. "." .. remain ..                string.rep("0", decPlaces - string.len(remain))  end  return formattedend

Post a screenshot when you've made the above changes for all your stocks and I'll decide whether or not to incorporate the changes into the DA version of MarketPrices.

Statistics: Posted by Mordasius — Today, 9:08 am



Viewing all articles
Browse latest Browse all 1572

Trending Articles