Hello everyone, I've created a derma slider that i would like to return the number thats selected, My code is this:
[CODE]local taxSlider = vgui.Create( "DNumSlider", taxMenu )
taxSlider:SetPos( 75, -10)
taxSlider:SetSize( 150,100 )
taxSlider:SetText("Tax amount")
taxSlider:SetDecimals( 0 )
taxSlider:SetMin( 0 )
taxSlider:SetMax( 100 )
taxSlider:SetValue( 0 )
local taxConfirm = vgui.Create("DButton", taxMenu)
taxConfirm:SetPos( 110 , 100 )
taxConfirm:SetSize( 80, 30 )
taxConfirm:SetText("Confirm tax")
function taxConfirm:DoClick()
taxAmount = taxSlider:GetValue()
print(taxAmount)
end[/CODE]
Now thats all fine, Except once i click Confirm tax, I get numbers like this:
[CODE]69.642857142857
[/CODE]
How can i make it so it returns the whole number, No rounding down or up.
math.round :v:
How simple can that be?
[QUOTE=LUModder;46479361]math.round :v:
How simple can that be?[/QUOTE]
Like i said, I dont want to use rounding because the numbers can be 64.9 and then it returns 65, And that doesnt return the true value the user specified.
[QUOTE=Adiminium;46479398]Like i said, I dont want to use rounding because the numbers can be 64.9 and then it returns 65, And that doesnt return the true value the user specified.[/QUOTE]
You know that math.Round takes also an second arg, which determinates how many decimal places to round to?
so i think you are looking for math.Round(value, 1)
[QUOTE=Giraffen93;46479505][lua]taxSlider.ValueChanged = function(pSelf, fValue)
pself:SetValue(math.Round(fValue))
end[/lua][/QUOTE]
That just makes the slider display a constant 0
[editline]13th November 2014[/editline]
[QUOTE=Tomelyr;46479516]You know that math.Round takes also an second arg, which determinates how many decimal places to round to?
so i think you are looking for math.Round(value, 1)[/QUOTE]
Yes I'm aware and i have tried it, Also that would return number.1stdecimalplace and that isnt what i need.
Honestly, the SetDecimals function should take care of this automatically; I may do a pull request to automate this unless someone else does it first.
[QUOTE=Acecool;46479543]Honestly, the SetDecimals function should take care of this automatically; I may do a pull request to automate this unless someone else does it first.[/QUOTE]
Yeah I've tried to use SetDecimals function, It just doesnt seem to work.
[lua]local intstring = tostring(fValue)
local intstring2 = string.Explode(".", intstring)
intstring = tonumber(intstring2[1])
[/lua]
something something like this then? i can't find an proper function to strip the demicals out, so the way over string.
[QUOTE=Tomelyr;46479567][lua]local intstring = tostring(fValue)
local intstring2 = string.Explode(".", intstring)
intstring = tonumber(intstring2[1])
[/lua]
something something like this then? i can't find an proper function to strip the demicals out, so the way over string.[/QUOTE]
Ill try it out later if there's no shorter method.
I think someone added math.Truncate to remove decimals. There is also math.floor and math.ceil ( which either is the whole number, or is stepped up or down depending on the decimal value and the function.
[QUOTE=Adiminium;46479587]Ill try it out later if there's no shorter method.[/QUOTE]
[LUA]
local num = 345.453234
local integral, fractional = math.modf( num )
print( integral, fractional )
--> 345 0.453234
[/LUA]
[QUOTE=Adiminium;46479521]That just makes the slider display a constant 0[/QUOTE]
[lua]local SkinSlider = vgui.Create( "Slider", BodygroupPanel )
SkinSlider:Dock(TOP)
SkinSlider:SetMin(1)
SkinSlider:SetMax(NPCModel.Entity:SkinCount())
SkinSlider:SetDecimals(0)
SkinSlider:SetValue(skin)
SkinSlider.OnValueChanged = function( panel, value )
skin = value
NPCModel.Entity:SetSkin(skin)
end[/lua]
Direct copy from my code, couldn't remember it earlier. It probably works for DNumSlider too.
Why not math.floor() or math.round(x-0.5)?
I don't understand, why do you need a function to strip the decimal out? Just math.floor it...
I would understand if you needed the decimal part, but if you just need the whole number, just floor it?
[QUOTE=AnonTakesOver;46485135]I don't understand, why do you need a function to strip the decimal out? Just math.floor it...
I would understand if you needed the decimal part, but if you just need the whole number, just floor it?[/QUOTE]
Obviously the 1st thing i would do is attempt to round it.
Whenever i use math.floor() if the number was say 33 it would return 32? thats swhy i made this thread and said i didnt want to round..
Use the method rejax posted. Does exactly what you're looking for.
[QUOTE=Adiminium;46495794]Obviously the 1st thing i would do is attempt to round it.
Whenever i use math.floor() if the number was say 33 it would return 32? thats swhy i made this thread and said i didnt want to round..[/QUOTE]
math.floor rounds down - so 32.987 would be 32, even if your control is displaying a value of 33.
Any solution we give you is going to "round" the number because the actual value is different from what is being displayed by your control - when you set the decimals to zero it rounds the value automatically so it will always show as the nearest whole number.
math.floor / ceil functions don't round, they step. It works essentially just like math.Truncate ( except truncate allows you to specify the number of decimals to retain ).
Any time you have a decimal, it steps to the whole number below while ceil steps up.
number / floor / ceil
0 / 0 / 0
1 / 1 / 1
1.0000000000000001 / 1 / 2
1.9999999 / 1 / 2
It doesn't matter what the decimal is, if the number isn't whole then floor / ceil will make it whole.
Sorry, you need to Log In to post a reply to this thread.