Adjust the value of cents

Asked

Viewed 128 times

-15

I have a variable with the following information: 100.3

What better alternative to make 100.3 to 100.03?

  • 9

    That doesn’t make much sense, at least it’s not rounding, it changes the value completely and makes the opposite of rounding.

  • @Maniero Ok, what’s the best alternative if I have to take this solution?

  • 1

    Realize that 100.3 and 100.03 are completely different values (if they were monetary values, for example, it would be a difference of 27 cents, far from being a "rounding"). Could [Edit] the question and make it a little more clear what it is trying to do and if possible put some more examples of numbers and their respective results? Maybe with a little more context we can better understand the problem.

  • 4

    Why do you need this? It doesn’t seem to make sense and it’s probably the wrong solution.

  • 5

    for this you only need to do n=n. replace(".",".0"); but nonsense that operation

  • Guys, I’m working on a figure, $186.9, I’d like to return, 186.09, how hard it is to understand the question?

  • 3

    @Josimara how hard it is to answer the questions we asked you?

  • @Maniero I just answered.

  • Where? I couldn’t find that answer.

  • 1

    You can even understand what you want to do, but you’re not giving to understand a practical example because it’s completely different 168.9 of 168.09. Is not seeking 168.90?

  • 1

    @Renan basically this, I’m not able to expose because I’ve never worked with this kind of value before...

  • 8

    We understand what you want to do, we’re just finding it strange the need to do it, because without a bigger context, it seems to be a meaningless operation. Maybe you are trying to solve a problem and thought that this solution is the best way, but there may be a better solution, and so I asked you to edit putting a little more context (in other words, I may be wrong, but looks like one XY problem). Don’t take our questions wrong, everyone is trying to help in the best way possible.

  • @hkotsubo understand, but you have to know that not always a person will be able to express themselves correctly if coming to a forum get a help

  • 1

    @Josimara I understand perfectly that it is not always easy to explain a problem. On the other hand, you have to understand that if the person cannot express himself correctly, not everyone will understand what he is saying, and consequently ask for more details and clarifications. Is part... :-)

  • 1

    @Josimara is very simple to say why you need to do this, you’re avoiding because this is probably a wrong solution to the problem. And alone we are wanting, fruitlessly, to help solve the real problem.

  • @Maniero Psé had an error, first the error start with myself... but I found a solution, https://answall.com/questions/251509/completer-casas-decimais-00-javascript

  • @Josimara Have any of the answers solved your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

Show 12 more comments

2 answers

7

It’s pure math (you don’t have to do tricks with string, it makes no sense to perform expensive conversions that makes more than one memory allocation unnecessary, aside from the semantics being incorrect, people need to stop doing what just works and understand what they’re doing and make decisions correct). That would be it, although it makes no sense:

var valor = 100.3;
var inteiro = Math.trunc(valor);
console.log(inteiro + ((valor - inteiro) / 10));

I put in the Github for future reference.

  • I’ve used it with math, I thought I’d find something deeper, but thanks!

  • 1

    instead of Math.trunc can be used parseint() which gives in the same.

  • 1

    @Hudsonph I only do gambiarras where it has advantage, if the semantics is all values I will not involve a function that was made to deal with string and by a philosophical fault the language coincidentally works. https://i.stack.Imgur.com/jgk8Q.png

  • @Maniero only one example, after all trunc is only (n - n % 1) and rest validations

  • Actually what I needed was a.toFixed(2) value, can correct me if the function is wrong...

  • 1

    @Josimara may be, but your question has no information on this, we have no way of knowing what you want. From the beginning we suspected she was wrong.

Show 1 more comment

6

Just do that:

var n = 100.3;
n=n.toString().replace(".",".0");
console.log(n);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.