There is a number. For example 0.66111

How to get js - 0.6 ?

3 answers 3

function cut(value, precision){ var dec = Math.pow(10, precision) return Math[(value > 0) ? 'floor' : 'ceil'](value * dec) / dec } 

For your case just apply a cut(0.66111, 1)

PS I want to note that I wrote the function of circumcision of a number, but not its rounding

UPD. Given the @VladD remark, the function has been improved for use with negative numbers.

  • omg: (0.66111) .toFixed (2); - lampa
  • one
    .toFixed(precision) Andrey, in your case .toFixed(precision) redundant - Specter
  • @lampa, be more attentive - Specter explained in his answer why it is impossible so - Yakovlev Andrei
  • @lampa, author's task from 0.66111 to get 0.6 . Your method will be 0.7 ... Feel the difference. You are mistaken. - Yakovlev Andrei
  • one
    it is also necessary to separately handle the case of a negative argument. Try your code for -0.66111 . ( hint ) - VladD

Elementary is:

 +0.66111.toFixed(1) // 0.7 +(0.66111.toString().substr(0,3)) // 0.6 

    @ModaL here I am a sucker, well hold then:

     Math.floor(0.66111*10)/10 

    As the shortest: D

    • +10 more to your points) Lucky the same)) - ModaL
    • @ModaL And you will not be sick, +100 :-) - lampa
    • @lampa, uhhhh! Here is this generosity! I will have to ^^ - ModaL
    • one
      @Spectre It's shorter, and just :-) - lampa
    • one
      In my opinion the most simple and useful answer, in javascript, the smaller the code, the faster the page loads. From such trifles in the overall picture, a huge foot wrapper is created, which can be squeezed once in 2. - Nov