CommandHelper

CommandHelper

46.5k Downloads

trim() should be able to cut off trailing or leading zeros

LadyCailinBot opened this issue ยท 9 comments

commented

CMDHELPER-3098 - Reported by macjuul

Since trim() cuts of trailing & leading white spaces, it would be handy if it would also cut off trailing/leading zeros in both integers and doubles

commented

Comment by PseudoKnight

Shouldn't that already happen if you do integer(@var) or double(@var)? It also should happen automatically any time you do math with the number.

commented

Comment by macjuul

Oh no I mean like in printing it so instead of messaging someone 1.0 it would instead message 1
It would also change for example 42.1300 to 42.13

commented

Comment by PseudoKnight

I use round() for that. Though, I guess you're saying sometimes it'll be 1.0 and sometimes it'll be 1.1, and you want it to display as integer in the one case. 42.1300, however, would always display as 42.13 unless it was explicitly a string.

commented

Comment by macjuul

Well thanks! But still, I don't exactly understand it. I'll make some examples:
1 will stay 1
1.5 will stay 1.5
1.0 will change to 1
Essentially the unnecessary zeros would be cut off

EDIT:
I currently use this to get the desired effect:

proc _trim_zeros(@n) {
@s = split('.', @n)
if(array_size(@s) == 1) {
return(@n)
}
if(@s[1] == 0) {
return(floor(@n))
}
return(@n)
}

commented

Comment by PseudoKnight

Where are you getting this number from?

commented

Comment by PseudoKnight

Also, I already mentioned that in the second sentence of my last response.

commented

Comment by PseudoKnight

A better way, in this case, would be to just test if the double is_integral().

commented

Comment by macjuul

The number is coming from a command variable. First the variable gets devided by 10 and later on it (sometimes) gets multiplied by 10 again, so it might come out as something like 8.0, but I want it to show up as 8

commented

Comment by LadyCailin

I'm not sure this is a common enough operation to want to add it to the core. A proc would be much better for this task, for your own use. Closing as won't fix.