0

Ok, so i have a RGB selector, this RGB selector takes the normal RGB output of whatever a user selects and divides the R, G & B values by 255.00

For example, RED (255,0,0) would be equal to: (1,0,0)

Another example, This normal RGB output (134,14,197) would get converted to this: (0.5254901960784314,0.054901960784313725,0.7725490196078432)

Now how would i take these values and make it so it only shows THREE numbers after the decimal point? So for example the below RGB value (0.5254901960784314,0.054901960784313725,0.7725490196078432) would be equal to this: (0.525,0.054,0.772)

I've tried using the toFixed() function and the toPrecision() without any success. Sorry for this question, it's just that i'm kindof a noob and have been researching/trying to find an answer to this for the past few days.

EDIT: Here is an example of what i've tried:

StoreR=R_OUT.value/255.00.toFixed(2);
//NOTE: R_OUT.value is equal to the RED value, for example: 0.5254901960784314
RevTech
  • 35
  • 2
  • 9
  • Please show the code you tried – Sterling Archer Apr 08 '16 at 19:52
  • @abc123 no, this is not a duplicate. i DO NOT want to round ANY numbers........... And i have added a bit of code showing what i tried – RevTech Apr 08 '16 at 19:56
  • @RevTech that isn't an example...you need to show your work, not the single line calling `toFixed`. Give us enough code to replicate your issue or at least see the issue. No where in your answer did you say you don't want to round. And there isn't a way to do precision on decimals without rounding off the excess either `roof` or `floor`. With the above information all I can do is point you to the above question and answer which makes this question a duplicate. – abc123 Apr 08 '16 at 19:58
  • @abc123 LOL. how is my code example "not enough"?? the example i've provided: StoreR=R_OUT.value/255.00.toFixed(2); is all anyone needs to see to see what im doing wrong. maybe this will be easier for you to understand: StoreR=0.091229478365/255.00.toFixed(2); either way what i've provided is entirely enough to see the problem in my code, you just don't seem to beable to understasnd – RevTech Apr 08 '16 at 20:03
  • 1
    I would suggest to use like this `val = parseFloat((R_OUT.value/255.00).toFixed(2))` – Rajesh Kumar Apr 08 '16 at 20:04
  • @RajeshKumar Thank you sooo much!!!! It works perfectly :) <3 – RevTech Apr 08 '16 at 20:07

1 Answers1

2

Here you go:

StoreR = (R_OUT.value/255.00).toFixed(2);

You need the brackets like above otherwise this basically what is happening:

StoreR = R_OUT.value / (255.00.toFixed(2));

In other words, toFixed is being applied to 255.00 unless you specify that you want to first devide R_OUT.value by 255.00 before applying the toFixed function.

nathan felix
  • 396
  • 1
  • 8