I have generic list -GasPropList - in that i want the sum of the all individual items value.
All Varaibles are in Double datatype.
For Each gasprop In GasPropList
gasprop.OUTGi = gasprop.NameVal * gasprop.GasGi / 100
gasprop.OUTPci = gasprop.NameVal * gasprop.Pci / 100
gasprop.OUTTci = gasprop.NameVal * gasprop.Tci / 100
gasprop.OUTVi = gasprop.NameVal * gasprop.NHVi / 100
gasprop.OUTGVi = gasprop.NameVal * gasprop.GHDVi / 100
Next
From this i want to calculate sum of OUTGi, OUTPi etc.
I saw Linq Sum function, but don't know how to use for my case.
E.g.
Dim OUTGiSum = GasPropList.Sum(Function(gp) gp.OUTGi)
I'll leave it to you to do the others.
Related
Background
We have 5 items in our portfolio along with the quantity sold, margin and prices of every item from last month. Predicted quantity sold of every item is dependent on its own price (through own price elasticity) and price of every other item (cross price elasticity). I am trying to write an pyomo script to increase prices of every item optimally and maximize the profit.
Example for 5 items after calculating a 5*5 matrix for elasticities.
Predicted quantity sold for every item is following function: Pred(Q1) = exp(p1 * e11 + p2 * e12 + ...... + p5 * e15)
Margin for every item is available.
Current Net profit : P1 * Q1 * Margin1 + ......... + P5 * Q5 * Margin5 = 1000
Objective : Max(P1 * (1+change p1) * Pred(Q1) * Margin 1 + ......... + P5 * (1+change p5) * Pred(Q5) * Margin 5
Constraints:
We can change the price of every item by [-5% , +10%]
Predicted quantity sold of every item with new prices should not change by
more than [-5% , +10%] of current quantities
Maximum profit achieved should be greater than 1000
All prices and quantities should be positive
Need help with programming it in pyomo with n variables and constarints, ipopt is what I think of using but open for recommendation from experts.
#Parameters
model.N = pyo.Param(model.a, initialize = qty_dict)
#Function that returns bounds on variable x
def bounds_for_y(model,l):
return (round(model.N[l]*0.95,0),round(model.N[l]*1.1,0))
#Here l is just a placeholder for set a
#Variable Declaration
model.qty = pyo.Var(model.a, domain = pyo.Integers, bounds = bounds_for_y )
Similarly for price:
#Variable declaration
model.prices = pyo.Var(model.a, domain = pyo.Reals, bounds = bounds_for_x, initialize = price_dict)
enter code here
I have to check how many hundreds are there in a number and translate that number to letters. For example the number 700. I have done the following code:
DATA(lv_dmbtr) = ZDS_FG-DMBTR. //Declared local variable of type DMBTR, thus DMBTR=700.
lv_dmbtr = ZDS_FG-DMBTR MOD 100. //Finding how many times 700 is in 100 via MOD and putting the value in lv_dmbtr.
IF lv_dmbtr LE 9. //The value is less or equal than 9(if larger means that the DMBTR is larger than hundreds,
e.g. 8000)
lv_hundred = lv_dmbtr / 100. // Divide the 700 with 100, taking the number 7.
lv_hundred_check = lv_hundred MOD 1. // Then taking the value of 7 into the new variable, done in case the
lv_hundred is a decimal value, e.g. 7.32.
IF lv_hundred_check > 0.
CALL FUNCTION 'SPELL_AMOUNT'
EXPORTING
amount = lv_hundred_check
* CURRENCY = ' '
* FILLER = ' '
LANGUAGE = SY-LANGU
IMPORTING
in_words = lv_hundred_string // the value is put in the new string
EXCEPTIONS
not_found = 1
too_large = 2
OTHERS = 3.
ENDIF.
Now when I debugg the code, all the variables have the value 0. Thus, lv_dmbtr, lv_hundred, lv_hundred_check all have the value 0.
May anyone of you know where the problem may be?
Thank you in advance!
Sorry for writing a lot in the code, just wanted to clarify as much as I could what I had done.
yes so I want to display the value of a specific number 700-> seven, 1400-> four.
So the basic formula to get the hundred in a number is the following: Find out how many times 100 fits completely into your number with integer division.
99 / 100 = 0
700 / 100 = 7
701 / 100 = 7
1400 / 100 = 14
1401 / 100 = 14
Now you can simply take this number MOD 10 to get the the individual hundreds.
0 MOD 10 = 0
7 MOD 10 = 7
14 MOD 10 = 4
Keep in mind that ABAP, in contrast to many other programming languages, rounds automatically. So in code this would be:
CONSTANTS lc_hundred TYPE f VALUE '100.0'.
DATA(lv_number) = 1403.
DATA(lv_hundred_count) = CONV i( floor( ( abs( lv_number ) / lc_hundred ) ) MOD 10 ).
I have a product table where I save the price and the amount of profit.
I want to display the amount of time displayed differently
for example :
price : 8.08
profit : 8
Presentable : 8.08 + (8.08 * 8 / 100) = 8.7264
Now I want the answer to be two decimal places and rounded up.
And the answer should be as follows:
Presentable : 8.08 + (8.08 * 8 / 100) = 8.73
The "C" currency format specifier should do the trick for you.
Dim amount As Decimal = 123.456
System.Console.WriteLine(amount.ToString("C", System.Globalization.CultureInfo.CurrentCulture))
' Prints : $123.46
More info here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#CFormatString
There is a also a Round method, where you can specify to how many decimal places you would like to round the number. So in your case of rounding to 2 decimal places you could make a call like:
System.Math.Round(amount, 2, System.MidPointRounding.AwayFromZero)
I have the original price of a commodity and its discounted price.
I currently have some code but it is not working properly.
For example: The price of a pre-discounted commodity is 853.2 $.
The discounted price is 349 $.
That is a 59% discount, but my code shows the wrong numbers.
Dim a1 As String = (Val(pre-discounted_price) - Val(discount_price) * 100 / Val(discount_price)).ToString
Dim a2 As Integer = (853.2 - 349) * 100 / 349
The formula for calculating how much you need to discount something by, to reach a new price is:
100 * (originalprice - newprice) / originalprice
100 * (852.3 - 349) / 852.3 = 59 (0 decimal places)
Checking:
852.3 - (852.3 * 0.59) = 349 (approx)
Your code went wrong because you divided by the newprice
I have got a large amount of rows with different values in it, from 1000s to 1,000,000s.
This is data that has come across to me in bytes and i need to convert it to Kb or Mb respectively.
I could divide all the values by 1M and get all the data en Mb but i want to have the data in kb and Mb.
I tried using the MOD function but that wont do the trick as all the numbers are going to be divisible by 1K and 1M so i am a bit stuck!
here is a sample of the data that i get:
16000000
220000
2048000
2048000
230000
16000000
230000
16000000
220000
230000
so what i need is that if the cell contains 6 zeros then divide by 1M or if the cell contains 3 zeros devide by 1000.
I will add concatenation to each individual result in order to get the data differentiated.
This is the function you are looking for :
Public Function SizeInStr(ByVal Size_Bytes As Double) As String
Dim TS()
ReDim TS(4)
TS(0) = "b"
TS(1) = "kb"
TS(2) = "Mb"
TS(3) = "Gb"
TS(4) = "Tb"
Dim Size_Counter As Integer
Size_Counter = 0
If Size_Bytes <= 1 Then
Size_Counter = 1
Else
While Size_Bytes > 1
Size_Bytes = Size_Bytes / 1000
''Or
'Size_Bytes = Size_Bytes / 1024
Size_Counter = Size_Counter + 1
Wend
End If
SizeInStr = Format(Size_Bytes * 1000, "##0.0#") & " " & TS(Size_Counter - 1)
''Or
'SizeInStr = Format(Size_Bytes * 1024, "##0.0#") & " " & TS(Size_Counter - 1)
End Function
Use it simply like this :
Private Sub Test_SizeInStr()
MsgBox SizeInStr(1000000)
End Sub
If you are looking for some VBA code you might use this Function:
Public Function yourFunction(ByVal number)
If number > 1000000 Then
'number as MB
yourFunction = number / 1000000
'or use the following to add MB
'yourFunction = (number / 1000000) & " Mb"
Else
'number in kB
yourFunction = number / 1000
'or use the following to add kB
'yourFunction = (number / 1000) & " Kb"
End if
End Function
If you are looking for an Excel-Formula you might use this Function (original value in A1), put this formula in another column (for example column B)
=IF(A1>1000000,A1/1000000,A1/1000)
or with concatenation:
=IF(A1>1000000,CONCATENATE(A1/1000000," Mb"),CONCATENATE(A1/1000," Kb"))
I would avoid changing the numbers themselves, and use a number format instead. If you use a function or macro to modify the number, then you will no longer be able to eg. sum them up.
This number format does exactly what you need, but leaves the actual numbers untouched, in bytes, so you can still use them in formulas:
[>1000000]0,,"Mb";[>1000]0,"kb";0"b"
Note that this can be environment language dependent. For me with Hungarian regional settings, spaces must be used instead of commas:
[>1000000]0 "Mb";[>1000]0 "kb";0"b"
What space/comma does is each of them "hides" three digits from the end.
For any number n, apply this
If n Mod 1000 = 0 Then
n = n / 1000
End If
twice. On the first run it will convert millions to thousands, and thousands to units. On the second run it will converts any thousands to units.