Jump to content
  • 0
Jason

Adding two floating point numbers in javascript

Question

I’m working on a script to blend between two values and for some reason I keep ending up with a number that doesn’t make any sense.

It looks something like:

var a = 1.00;
var b = 0.0113;
var sum = a+b;

 

the result is 1.00.0113;

Shouldn’t I be getting 1.0113?

 

Share this post


Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0

It turns out that the Javascript keeps thinking that I'm trying to add two strings together.. that's why I'm getting 1.00.0113!

pug GIF

So, the crazy solution is to trick javascript into understanding these are numbers, not strings.  I tried a bunch of things.. and believe it or not, this is what worked:

var a = 1.00;
var b = 0.0113;
var sum = (1*a) + (1*b);

Yeah.

 

That worked!

simon cowell facepalm GIF

You don't even want to know how many times I searched for answers until I found that one.

🙂

Share this post


Link to post
Share on other sites
  • 0
6 hours ago, Andrei Nadin said:

You can also try..


var a = parseFloat(1.00);
var b = parseFloat(0.0113);
var sum = a+b;

Seems a little nicer.... <yuk>

Hah!  You know what.. I realized I had tried parseFloat, but I was only doing it on one of the variables, which is why it wasn't working.  Ugh.  Thank you for a much clearer solution than my hacky one. 🙂

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • By Jason
      I've recently updated my github repo with a new tool to make it easier to do breakdowns - check it out and enjoy! 🙂
      https://github.com/shhlife/harmony

    • By Jason
      View File NC_CreateNote.js
      This script takes adding a Note layer to harmony from a multi-step process to a single button click!
      The script will:
      Automatically create a drawing layer called Note (or Notes_# if there's more than one) Colors the drawing layer so it stands out from the other layers in your scene Connects the notes layer to the top of your comp stack. Automatically switches you to the pencil tool so you can start drawing immediately.  
      NC_CreateNote.mp4 Submitter Jason Submitted 11/21/2018 Category Harmony  
    • By Jason
      I've put together a github repo that contains all the scripts I've written for ToonBoom Harmony - so you can get them all in a single place. 
      I'll keep posting them here as well, but if you're interested in helping out, making them better, or just pulling them all at once, feel free to grab them here!
      https://github.com/shhlife/harmony
    • By Jason
      This script takes adding a Note layer to harmony from a multi-step process to a single button click!
      The script will:
      Automatically create a drawing layer called Note (or Notes_# if there's more than one) Colors the drawing layer so it stands out from the other layers in your scene Connects the notes layer to the top of your comp stack. Automatically switches you to the pencil tool so you can start drawing immediately.  
      NC_CreateNote.mp4
×