decimal

JavaScript: Decimal Values

这一生的挚爱 提交于 2019-12-29 04:30:12
问题 What can I use as a decimal type in JavaScript? It's not supported ( 0.1 + 0.2 !== 0.3 ), and I need it for representing exact values in a banking/financial application. See The State and Future of JavaScript for a good read and the dirty details behind JavaScript and it's (lack of) support for decimal arithmetic. By "decimal", I mean either: infinite-range and arbitrary-precision (like BigDecimal in Java), or limited range and precision, but suitable for financial calculations (like decimal

javascript number precision without converting to String

折月煮酒 提交于 2019-12-29 02:11:10
问题 I am developing a REST API and returning JSON. One of the fields is called submissionPercent and I need it to be a number but with exactly 2 decimal places If the submissionPercent is 20, I need to return 20.00. If the submissionPercent is 20.238, I need to return 20.24. But submissionPercent should be a number not a String. If I use toFixed or toPrecision , then what I get is a String. If possible, how do I achieve this? 回答1: var n = 20.238; Math.round(n * 100) / 100 // => 20.24 Or more

Oracle number to C# decimal

给你一囗甜甜゛ 提交于 2019-12-29 01:33:11
问题 I know there are several threads and posts regarding this issue in the internet and I've read them (not every article, I have to admit) but none of them did fully satisfy me. My situation: I'm using ODP.net (dll version 2.111.6.0) to access the Oracle DB (version 10 + 11) and a DataReader to retrieve the data (.NET 3.5, C#). Using this code results in a ' System.OverflowException (Arithmetic operation resulted in an overflow.) ' decimal.TryParse(oraReader.GetOracleDecimal(0).Value.ToString(),

Clarification on the Decimal type in Python

空扰寡人 提交于 2019-12-28 11:45:09
问题 Everybody know, or at least, every programmers should know, that using the float type could lead to precision errors. However, in some cases, an exact solution would be great and there are cases where comparing using an epsilon value is not enough. Anyway, that's not really the point. I knew about the Decimal type in Python but never tried to use it. It states that "Decimal numbers can be represented exactly" and I thought that it meant a clever implementation that allows to represent any

Java negative int to hex and back fails

风格不统一 提交于 2019-12-28 06:22:07
问题 public class Main3 { public static void main(String[] args) { Integer min = Integer.MIN_VALUE; String minHex = Integer.toHexString(Integer.MIN_VALUE); System.out.println(min + " " + minHex); System.out.println(Integer.parseInt(minHex, 16)); } } Gives -2147483648 80000000 Exception in thread "main" java.lang.NumberFormatException: For input string: "80000000" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:459) at

NSNumberFormatter with comma decimal separator

一曲冷凌霜 提交于 2019-12-28 04:12:08
问题 I tried to convert an NSString like "12000.54" into "12.000,54". I wrote an NSNumberFormatter instance. NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease]; [formatter setFormatterBehavior:NSNumberFormatterBehavior10_4]; [formatter setNumberStyle:NSNumberFormatterDecimalStyle]; [formatter setGroupingSeparator:@"."]; [formatter setDecimalSeparator:@","]; But when I NSLog this : NSLog(@"%@",[formatter stringFromNumber:[formatter numberFromString:value]]); It prints

How can i format decimal property to currency

强颜欢笑 提交于 2019-12-28 03:51:20
问题 I want to format the value in the getter and return a formated currency value. Is this possible or do i need to declare the property as a string and then use string.format. 回答1: Properties can return anything they want to, but it's going to need to return the correct type. private decimal _amount; public string FormattedAmount { get { return string.Format("{0:C}", _amount); } } Question was asked... what if it was a nullable decimal. private decimal? _amount; public string FormattedAmount {

Bash shell Decimal to Binary base 2 conversion

亡梦爱人 提交于 2019-12-28 02:44:10
问题 I'm looking for an easy way in Bash to convert a decimal number into a binary number. I have variables that need to be converted: $ip1 $ip2 $ip3 $ip4 Is there a simple method to do this without looking at every individual number? I would prefer not to have to write a lot of code. 回答1: You can use bc as: echo "obase=2;$ip1" | bc See it 回答2: Convert decimal to binary with bash builtin commands (range 0 to 255): D2B=({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}) echo ${D2B[7]} 00000111 echo

How to truncate decimal value without loosing precision

偶尔善良 提交于 2019-12-25 21:24:09
问题 I have current variable php: $latlng = "<a href='http://www.google.com/maps/place/".$lat.",".$lng."/@".$lat.",".$lng.",".$zoom."' target='_blank'>Lat ".$lat." Lng ".$lng."</a>"; echo output is as following example: i want truncate it so at end i want have following result: I have tried following code but it cut only final part: $cut_string = substr($latlng,0,strpos($latlng,'</a>')-12); echo "$cut_string <br/>\r\n"; Thanks 回答1: I think I understand your problem. You want the link with full

Using Math.round in javascript adds weird number of 0s at the end [duplicate]

☆樱花仙子☆ 提交于 2019-12-25 18:23:56
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Is JavaScript’s Math broken? I'm using Math.round to round the number and for some reason it adds weird number of 0s at the end which should not be there. Here is my code: return Math.round($digit * 1000) / 1000; i want numbers to have 3 decimal points example: Math.round(29.469 * 1000) / 1000 = returns this value: 29.469000000000023 cant seem to figure out why. is there a different way of rounding decimals to