josdejong / mathjs

An extensive math library for JavaScript and Node.js
https://mathjs.org
Apache License 2.0
14.45k stars 1.24k forks source link

Make option to have number in full form, instead of standard form #1742

Closed ghost closed 3 years ago

ghost commented 4 years ago

Say if we did this as a calculation:

2837593480275824/5468930457689534768957689350476859760345768945076803945654*548345097438534853535

The output is 2.845127553679215e-22 as you can see, it is in standard form.

I have tried converting the answer to a string using .toString() however the output is still 2.845127553679215e-22. I want it to show the full-on value, instead of just being in standard form.

Thanks in advance.

Here's my existing code (my discord bot)

        if (!args.length) {
            return message.channel.send(`${process.env.re} You need to specify a calculation!`)
        }
        let resp;
        try {
                resp = math.evaluate(args.join(' ')).toString();
        } catch (error) {
                return message.channel.send("Invalid Calculation")
        }
        message.channel.send("", {
            embed: new Discord.RichEmbed()
            .setDescription(resp)
            .setColor(jsonColor)
        });
josdejong commented 4 years ago

Are you working with BigNumbers? Regular numbers have a precision of about 16 digits only.

ghost commented 4 years ago

Yes. It's a part of my discord bot and want all numbers to be shown in their full form

On Sat, 15 Feb 2020, 11:40 Jos de Jong, notifications@github.com wrote:

Are you working with BigNumbers? Regular numbers have a precision of about 16 digits only.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/josdejong/mathjs/issues/1742?email_source=notifications&email_token=ALXVSWBGCSORV6WLF74WM2DRC7IDNA5CNFSM4KVGFGI2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEL3IN3Y#issuecomment-586581743, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALXVSWCSWNZYFB4W2E5H5HDRC7IDNANCNFSM4KVGFGIQ .

--

*Company and disclaimer information related to this email can be found here. https://sites.google.com/aetinet.org/academies-enterprise-trust/email-disclaimer

*

josdejong commented 4 years ago

I think you're not actually working with BigNumbers that should give the following output:

math.config({ number: 'BigNumber' })
const result = math.evaluate('2837593480275824/5468930457689534768957689350476859760345768945076803945654*548345097438534853535')
console.log(result.toString())
// "2.845127553679215676780038456685211161450427105465427783234459329e-22"

BigNumbers are configured with 64 digits by default, this is configurable.

You can also look into the function format to customize how the output is formatted (instead of using .toString()).

ghost commented 4 years ago

Thanks, I'll try that right now

On Sat, 15 Feb 2020, 13:10 Jos de Jong, notifications@github.com wrote:

I think you're not actually working with BigNumbers that should give the following output:

math.config({ number: 'BigNumber' })const result = math.evaluate('2837593480275824/5468930457689534768957689350476859760345768945076803945654*548345097438534853535')console.log(result.toString())// "2.845127553679215676780038456685211161450427105465427783234459329e-22"

BigNumbers are configured with 64 digits by default, this is configurable.

You can also look into the function format to customize how the output is formatted (instead of using .toString()).

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/josdejong/mathjs/issues/1742?email_source=notifications&email_token=ALXVSWDVWJ3WMZSW7TEJKATRC7SVVA5CNFSM4KVGFGI2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEL3KGEI#issuecomment-586588945, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALXVSWF4CAEJ2IASKC5XCWLRC7SVVANCNFSM4KVGFGIQ .

--

*Company and disclaimer information related to this email can be found here. https://sites.google.com/aetinet.org/academies-enterprise-trust/email-disclaimer

*

ghost commented 4 years ago

It's just saying that it's an invalid calculation

        try {
                math.config({ number: 'BigNumber' })
                const result = math.evaluate(args.join(' '))
            message.channel.send("", {
                embed: new Discord.RichEmbed()
                .setDescription(resp.toString())
                .setColor(jsonColor)
            })
        } catch (error) {
                return message.channel.send(process.env.re + "Invalid Calculation")
        }

(when i input the same calculation as before)

ghost commented 4 years ago

the args.join(" ") is the same as the calculation we were doing earlier 2837593480275824/5468930457689534768957689350476859760345768945076803945654*548345097438534853535

ghost commented 4 years ago

just realised i made an error above, fixed it, but I keep getting this error now

Error: The global config is readonly. 
Please create a mathjs instance if you want to change the default configuration. 
Example:

  import { create, all } from 'mathjs';
  const mathjs = create(all);
  mathjs.config({ number: 'BigNumber' });
ghost commented 4 years ago

kk. I figured it out and got to here.

        const { create, all } = require('mathjs')
        const math = create(all);

However, even with the math.config({ number: "BigNumber" }), it still produces my result in standard form and not in its full form

ghost commented 4 years ago

Instead, it was 2.845127553679215676780038456685211161450427105465427783234459329e-22

josdejong commented 4 years ago

You can use math.format to choose a different way of formatting.

ghost commented 4 years ago

And format it to what? Or jsut the math.format method

On Sat, 15 Feb 2020, 18:32 Jos de Jong, notifications@github.com wrote:

You can use math.format to choose a different way of formatting.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/josdejong/mathjs/issues/1742?email_source=notifications&email_token=ALXVSWAOVQMOPIQF2ZCXUIDRDAYMZA5CNFSM4KVGFGI2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEL3T4MY#issuecomment-586628659, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALXVSWAPTE243FTS6MXP6RLRDAYMZANCNFSM4KVGFGIQ .

--

*Company and disclaimer information related to this email can be found here. https://sites.google.com/aetinet.org/academies-enterprise-trust/email-disclaimer

*

josdejong commented 4 years ago

What output would you expect instead of 2.845127553679215676780038456685211161450427105465427783234459329e-22?

ghost commented 4 years ago

One without the e-22. I don't want it in standard form or shortened

On Sat, 15 Feb 2020, 19:26 Jos de Jong, notifications@github.com wrote:

What output would you expect instead of 2.845127553679215676780038456685211161450427105465427783234459329e-22?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/josdejong/mathjs/issues/1742?email_source=notifications&email_token=ALXVSWENNEALMUU6EEO2FNTRDA6WZA5CNFSM4KVGFGI2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEL3VANA#issuecomment-586633268, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALXVSWH4KTWILHN6JX2OAMLRDA6WZANCNFSM4KVGFGIQ .

--

*Company and disclaimer information related to this email can be found here. https://sites.google.com/aetinet.org/academies-enterprise-trust/email-disclaimer

*

josdejong commented 4 years ago

You can use math.format to do that, just read the docs and play around with it, I think you can figure it out 😄 .