• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

GMS 2.3+ How to Floor Number not up to integer but to number.x ???

Petrik33

Member
Okay, probably the question is explained not teh best in the theme, so: Say, I have number 90.0777, so how do I floor it until 90.1 using matehmatical function? Yeah, so I guess the question is pure maths, I am just not very good at it, so I hope there is anyone who knows how to help me, cause when I try searching for it in Google it just gives me something I don't need, like the symbol for floor functions or graphs.
 

saffeine

Member
if it were me, i'd probably multiply it by 10 to bring it to one decimal place later, round it, then divide it again.
it should be noted that my methods are almost always convoluted for no reason at all so there might actually be a better way to do it.

GML:
var _number    = 90.0777;

_number        = _number * 10;         // to take you up to 900.777.
_number        = round( _number );     // to round it off to 901.
_number        = _number / 10;         // to bring it back down to 90.1.

// this can be shortened to the following:

var _number    = 90.0777;
_number        = round( _number * 10 ) / 10;
hope that's what you're looking for!
 

Petrik33

Member
if it were me, i'd probably multiply it by 10 to bring it to one decimal place later, round it, then divide it again.
it should be noted that my methods are almost always convoluted for no reason at all so there might actually be a better way to do it.

GML:
var _number    = 90.0777;

_number        = _number * 10;         // to take you up to 900.777.
_number        = round( _number );     // to round it off to 901.
_number        = _number / 10;         // to bring it back down to 90.1.

// this can be shortened to the following:

var _number    = 90.0777;
_number        = round( _number * 10 ) / 10;
hope that's what you're looking for!
damn, actualy, that's genius!) Thank you
 

Roderick

Member
I would do what @saffeine said, but one step further: Create a script so that you can do it all in one command. If you always need just one decimal place, you can just have the script contain the code above (modified to use the argument passed to it, and to return the value).

If you may need to round to different amounts, instead of dividing/multiplying by 10, use 10^n, where n is the number of decimal places. You'd need to pass two arguments to this script: the number to mod8fy, and the level of precision (n).

I still haven't upgraded from 1.4, and I know 2.x has changed how custom scripts work (specifically, how they handle arguments, IIRC), so I can't give you exact code. But I hope that I gave enough to figure it out yourself.
 

saffeine

Member
I would do what @saffeine said, but one step further: Create a script so that you can do it all in one command. If you always need just one decimal place, you can just have the script contain the code above (modified to use the argument passed to it, and to return the value).

If you may need to round to different amounts, instead of dividing/multiplying by 10, use 10^n, where n is the number of decimal places. You'd need to pass two arguments to this script: the number to mod8fy, and the level of precision (n).

I still haven't upgraded from 1.4, and I know 2.x has changed how custom scripts work (specifically, how they handle arguments, IIRC), so I can't give you exact code. But I hope that I gave enough to figure it out yourself.
it's funny you suggest that, because i was going to suggest the same thing, but didn't know if it'd be something that would be needed later.
i usually create a snap script / function to do exactly that, and after i posted my reply i realised it'd work with decimal places too.

< 2.3:
GML:
//    snap_number( number, snap_multiple ).
//    feel free to change round to floor or ceil, depending on what you need.

var _number = argument[0];
var _snap = argument[1];

_number = round( _number / _snap ) * _snap;
return _number;
2.3+:
GML:
//    feel free to change round to floor or ceil, depending on what you need.

function snap_number( _number, _snap ){
    _number = round( _number / _snap ) * _snap;
    return _number;
}
in both cases, you can either do something like number = snap_number( 90.0777, 0.1 ) to round to the nearest 0.1 decimal place, or just use snap_number( 90.0777, 0.1 ) as an argument directly.
 
Last edited:

Petrik33

Member
it's funny you suggest that, because i was going to suggest the same thing, but didn't know if it'd be something that would be needed later.
i usually create a snap script / function to do exactly that, and after i posted my reply i realised it'd work with decimal places too.

< 2.3:
GML:
//    snap_number( number, snap_multiple ).
//    feel free to change round to floor or ceil, depending on what you need.

var _number = argument[0];
var _snap = argument[1];

_number = round( _number / _snap ) * _snap;
return _number;
2.3+:
GML:
//    feel free to change round to floor or ceil, depending on what you need.

function snap_number( _number, _snap ){
    _number = round( _number / _snap ) * _snap;
    return _number;
}
in both cases, you can either do something like number = snap_number( 90.0777, 0.1 ) to round to the nearest 0.1 decimal place, or just use snap_number( 90.0777, 0.1 ) as an argument directly.
Actually, I have done jsut right this, and now I have read your comments suggesting doing that, nice!) But, still, you have one major improvement over mine: I am using the positive degrees of 10 as the snap numbers while you are using negative degrees and first divinding, and then multiplying, much more convenient, thank you
 

Petrik33

Member
One, big question, what I don't like I think is that in some cases it returns the right snapped number but, the zero at the end of it is still flawing everything, like I need 1.67, to be shown like 1.7, while the function returns 1.70, what can I do with that? @saffeine, @Roderick?
 

saffeine

Member
personally i have no clue how to remove the 0 from a number, if it's even possible. i think the way gamemaker handles numbers means that it results in that regardless.
if you want to remove it from a string though, for example, if you were drawing it to the screen as text, you could look for where the point is, and then just snip off whatever excess is there.

GML:
var _text   = "90.10"; // alternatively: var _text = string( 90.10 ); , you get the idea.
var _pos    = string_pos( ".", _text );
if( _pos != -1 ){ _text = string_copy( _text, 1, _pos + 1 ); }
just an example, of course. there might be a way to take it out of a number too, but i can't think of a practical reason for doing so.
i feel like for most mathematical applications, 90.1 will do the exact same as 90.10 due to both being the same number, just formatted differently.
 

Petrik33

Member
personally i have no clue how to remove the 0 from a number, if it's even possible. i think the way gamemaker handles numbers means that it results in that regardless.
if you want to remove it from a string though, for example, if you were drawing it to the screen as text, you could look for where the point is, and then just snip off whatever excess is there.

GML:
var _text   = "90.10"; // alternatively: var _text = string( 90.10 ); , you get the idea.
var _pos    = string_pos( ".", _text );
if( _pos != -1 ){ _text = string_copy( _text, 1, _pos + 1 ); }
just an example, of course. there might be a way to take it out of a number too, but i can't think of a practical reason for doing so.
i feel like for most mathematical applications, 90.1 will do the exact same as 90.10 due to both being the same number, just formatted differently.
yeah, you got it, big thank you, hust what I needed, although, probably the only solution in thsi case
 

TheouAegis

Member
That 0 is there because you are working with floating points and the value isn't explicitly one decimal place significant because of that. Your 1.7 would actually be 1.7000000001, for example.
 
Top