DjSt3rios Posted August 14, 2011 Share Posted August 14, 2011 Hello... I have a weird problem. I made a button to increase the volume of a sound, and show on the screen the percentage of the sound volume... i made it volume*100, and it starts with 100%, i press - button to decrease volume and its going 90%. then i press again - to go 80% but it appears this: 79.9999923 and some other numbers, instead of 80%... here is my code for increase/decrease volume: function increasevolume() setSoundVolume(song, getSoundVolume(song) + 0.10) killTimer(hidetimer) killTimer(hidetimer2) showmessage = 1 hidetimer2 = setTimer(hidemessage, 5000, 1) end function decreasevolume() setSoundVolume(song, getSoundVolume(song) - 0.10) showmessage = 1 killTimer(hidetimer) killTimer(hidetimer2) hidetimer = setTimer(hidemessage, 5000, 1) end function hidemessage() showmessage = 0 end and onClientRender i use this code: volume = getSoundVolume(song) dxDrawText("Volume: " .. tonumber(volume*100) .. "%", x1,y1-10,x2,y2, tocolor(255,0,0), 1.5, "pricedown","center","top", false,true,false) if someone has any idea, please tell me. thanks. EDIT: I made a command with outputChatBox to show the exact number of getSoundVolume... so i found that that with setSoundVolume(song, vol + 0.1, it doesnt really increase it by exactly 0.1... maybe any idea to fix this? Link to comment
qaisjp Posted August 14, 2011 Share Posted August 14, 2011 volume = getSoundVolume(song) dxDrawText("Volume: " .. math.floor(tonumber(volume*100)) .. "%", x1,y1-10,x2,y2, tocolor(255,0,0), 1.5, "pricedown","center","top", false,true,false) no problem Link to comment
DjSt3rios Posted August 14, 2011 Author Share Posted August 14, 2011 Actually i tried this right now, looks ok its just some times from 90 it goes to 79, which i dont really like, but it seems i dont have any other choice Link to comment
JR10 Posted August 14, 2011 Share Posted August 14, 2011 use math.round function math.round(number, decimals, method) decimals = decimals or 0 local factor = 10 ^ decimals if (method == "ceil" or method == "floor") then return math[method](number * factor) / factor else return tonumber(("%."..decimals.."f"):format(number)) end end Link to comment
qaisjp Posted August 14, 2011 Share Posted August 14, 2011 Not really a difference when it comes to *ceil*'ing and *floor*'ing > I had a problem which I only found a fix which was extremely dirty, if volume == 99% then volume = 100 end. This made sure it showed 100 when it was 99.xxxx Link to comment
JR10 Posted August 14, 2011 Share Posted August 14, 2011 math.round is better, if .4 or less then it's floor , if .5 or more then it's ceil. Link to comment
DjSt3rios Posted August 14, 2011 Author Share Posted August 14, 2011 Thanks JR10, but how should i make it? like instead of math.floor use this function? Link to comment
DjSt3rios Posted August 14, 2011 Author Share Posted August 14, 2011 I will give it a try, Thank you.. I will reply in a few minutes. Link to comment
DjSt3rios Posted August 14, 2011 Author Share Posted August 14, 2011 Thanks, works great! Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now