• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Extra decimal appearing

607

Member
Hello, I've got an issue when turning a real value read from an ini file into a string.
Take the following, with the example value being 4.4000000000:

string(ini_read_real("Example","Test",0)) gives "4.40"
string(floor(ini_read_real("Example","Test",0))) gives "4"
string(floor(10*ini_read_real("Example","Test",0))) gives "44"
string(floor(10*ini_read_real("Example","Test",0))/10) gives "4.40"

How do I get it to display 4.4? I could take the 0 from the string, but then I'd need to do an extra test to see if it's a zero after the decimal point and not before it, and it doesn't seem like a nice way anyway.
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
Code:
/// string_format_auto(number)
var s = string_format(argument0, 0, 15);
var d = string_pos(".", s);
if (d > 0) {
    for (var i = string_byte_length(s); i > d; i -= 1) {
        if (string_byte_at(s, i) != ord("0")) {
            return string_copy(s, 1, i);
        }
    }
    return string_copy(s, 1, d - 1);
}
return s;
and then use that instead of string()
 

607

Member
Woah, it's that complicated? I don't even understand that without further research. I'll just save the values in the ini as strings, then, to get around the issue.
 

607

Member
It somehow still doesn't work, even when storing the value using
ini_write_string("Example","Test",string(floor(10*global.variable)/10))
I don't know why this wouldn't work, and it's annoying. But I'll roll with it, as I'm making a 'simple' birthday present and not a professional product.

Edit:
Ah, I see.
Documentation said:
With this function you can turn any real number into a string. If the real number is an integer, it will be saved with no decimal places, otherwise, it will be saved with two decimal places.
That's inconvenient.

 

FrostyCat

Redemption Seeker
Woah, it's that complicated? I don't even understand that without further research. I'll just save the values in the ini as strings, then, to get around the issue.
How much research do you need to realize that all the script does is getting a full decimal expansion of the number passed into it, then trimming 0s off the right if it detects a decimal point?

And you don't even need to understand how it works in order to use it. The three slashes is YellowAfterlife's cue for you to put it in a script with the given name string_format_auto. So do as he suggests and then call it from your code like this:
Code:
ini_write_string("Example", "Test", string_format_auto(global.variable));
The solution is on a silver platter already and you're telling me that's complicated?
 

607

Member
How much research do you need to realize that all the script does is getting a full decimal expansion of the number passed into it, then trimming 0s off the right if it detects a decimal point?
None. It's obvious, given the context.
The solution is on a silver platter already and you're telling me that's complicated?
Yes. As I said, I am making a 'simple' birthday present, not a game with 60+ hours put into it. I don't want to use code written by others, here. :)

If I once do, I'll check out this thread again.
 
Top