question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request : toFixed() for vectors value

See original GitHub issue
Feature request

It would be nice to add a parameter to Vector3.round() and Vector3.roundToZero(), to ask for a number of decimal like Number.toFixed() in JS. If undefined, it would assume that an integer is needed, so it would not break anything.

Use case

I had to do my own function to do that, because the objects in my scene are user-transformed, then the transforms are exported as JSON. There is a big tolerance, so in order to reduce file size, I round the vector3 values. But rounding to integer is too much, that’s why I did my own function. I guess that it would also be useful in things like voxel-painter or Minecraft-likes.

I thought it might not be a big deal to add it to three.js, and it’s useful, hence my feature request. If you think it’s useless code, please close the issue.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:19 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
Mugen87commented, Nov 5, 2019

We should not use Number.toFixed() since it returns a string. It seems that number-to-string conversion just for rounding is no good approach.

https://stackoverflow.com/questions/2283566/how-can-i-round-a-number-in-javascript-tofixed-returns-a-string#comment47147413_14978830

Can you please test if this would work for you:

THREE.Vector3.prototype.round = function( digits ) {

    var e = Math.pow( 10, digits || 0 );

    this.x = Math.round( this.x * e ) / e;
    this.y = Math.round( this.y * e ) / e;
    this.z = Math.round( this.z * e ) / e;
        
    return this;

}

Live example (check browser console): https://glitch.com/~meowing-bougon

However, I don’t think this logic makes sense for Vector3.roundToZero().

1reaction
Mugen87commented, Nov 7, 2019

The semantics of Number.toFixed() and Vector3.toFixed() would be different since the native function returns a string. Hence, it’s confusing to reuse the same name.

I think enhancing the methods is okay. If we can solve the issue @sciecode mentioned.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Number.prototype.toFixed() - JavaScript - MDN Web Docs
The toFixed() method returns a string representation of numObj that does not use exponential notation and has exactly digits digits after the decimal...
Read more >
Learn the Syntax of JavaScript tofixed - eduCBA
The toFixed() method helps us specify the number of digits which we want to preserve while representing the value of that numeric object....
Read more >
Watch for changes | Sample Code - ArcGIS Developers
The sample shows how to watch for property changes. It watches the stationary property of the view when its value becomes true using...
Read more >
CPT 226 Final Flashcards - Quizlet
taxRate = taxRate;return invoice;}, When a client requests a dynamic web page, ... var getUserName = function() {var name = $("username").value;alert("Your ...
Read more >
Returning string from OpenLayers.Format.WKT write? [closed]
WKT().write( layer. ... ModifyFeature(layer, { title: "Select feature" })]); var map = new OpenLayers. ... selectedIndex].value; OpenLayers.Request.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found