简体   繁体   English

将负数转换为正数而不影响js中的小数点

[英]convert a negative number to positive without affecting decimal points in js

I need to convert a negative number to a positive number.我需要将负数转换为正数。 My attempt is as below.我的尝试如下。

Math.abs(-4.0);

It will return 4 as the answer.它将返回 4 作为答案。 But I need 4.0 as the answer.但我需要 4.0 作为答案。 Here I don't know the decimal places to be set.这里我不知道要设置的小数位。 For different numbers, there's a different decimal point value.对于不同的数字,有不同的小数点值。 Actually, that will totally depend on the web socket output.实际上,这完全取决于网络套接字输出。 According to the input value, the server-side defined different floating points which there's not any pattern according to our requirement.服务端根据输入的值定义了不同的浮点数,按照我们的要求没有任何模式。

So in js, is there any better way to handle this without getting decimal places separately?那么在js中,有没有更好的方法来处理这个而不单独获得小数位?

As mentioned in other comments, you won't be able to keep the precision.正如其他评论中提到的,您将无法保持精度。 toFixed() is definitely not a solution and this problem has valid case scenarios. toFixed()绝对不是一个解决方案,这个问题有有效的案例场景。

We had to do something similar where teachers would input a grade (90.05) and were expected to keep the same number of decimals for all their students inside of a single gradebook.我们不得不做一些类似的事情,老师会输入一个成绩 (90.05),并希望在一个成绩簿中为所有学生保留相同的小数位数。 Teachers grade differently depending of the situation:老师根据情况不同评分:

Teacher A | Teacher B | Teacher C
   90          90.5       4.95
   80          88.5       3.80
   75          80.0       5.00

One way to solve this, especially if the request comes as a string (from an input, http request, etc.), is to maintain the number as a string and return a string value.解决此问题的一种方法,尤其是当请求以字符串形式出现时(来自输入、http 请求等),是将数字保持为字符串并返回字符串值。

Note that this is a very crude implementation (our implementation is more fail-safe), but the idea is simple:请注意,这是一个非常粗略的实现(我们的实现更安全),但想法很简单:

 function absString(n) { numberString = n.toString(); if (numberString[0] === '-') { return numberString.substring(1); } else { return numberString; } } console.log(absString('-1')); console.log(absString('-2.0')); console.log(absString('2.0')); console.log(absString('-0.57'));

You can use the number toFixed method as follows:您可以使用数字toFixed方法如下:

 var decimalPlaces = 5 // Change this to change the number of decimals console.log(Math.abs(-4.0).toFixed(decimalPlaces))

If this is for display purposes only you can use the Intl.NumberFormat object to format your value appropriately.如果这仅用于显示目的,您可以使用Intl.NumberFormat对象适当地设置您的值的格式。 Example:例子:

Intl.NumberFormat(
  undefined,
  { signDisplay: 'never', minimumSignificantDigits: 3, maximumSignificantDigits: 20 }
).format(-4.06364635454000); // "4.06364635454"
Intl.NumberFormat(
  undefined,
  { signDisplay: 'never', minimumSignificantDigits: 1, maximumSignificantDigits: 20 }
).format(-4.0); // "4"
Intl.NumberFormat(
  undefined,
  { signDisplay: 'never', style: 'currency', currency: 'EUR' }
).format(-4.06364635454000); // "€4.06"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript 正则表达式正负十进制数 - Javascript regex positive negative decimal number Javascript:正则表达式用于正数和负数但没有小数点 - Javascript: regex for positive and negative number but no decimal point 将字符串中的负数转换为 JavaScript 中的负十进制数 - Convert negative number in string to negative decimal in JavaScript 在 JavaScript 中将负数转换为正数 - Convert a negative number to a positive one in JavaScript jQuery验证:以正数/负数和六位数2十进制归档的浮点数 - Jquery Validation : Floating number filed with Positive/Negative with 6 digit 2 decimal jquery - 仅允许在字段中验证负数、正数或十进制数 - jquery - allow only negative, positive or decimal number validation in field 0 到 10 范围内任何正或负十进制数的正则表达式 - Regular Expression for any positive or negative decimal number in a range of 0 to 10 如何使用Number()将字符串转换为数字而不丢失小数点? - How can I use Number() to convert a string to number without losing the decimal points? Javascript仅在ASP.NET TextBox中输入带有三位小数的正或负十进制数 - Javascript to enter only positive or negative decimal number with 3 decimal places into a ASP.NET TextBox Countup.js颜色编号,按语句(正,中性,负) - Countup.js color number by statement (positive, neutral, negative)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM