decimal

Round just decimal in Javascript

穿精又带淫゛_ 提交于 2019-12-11 13:36:32
问题 I have a value like that: 20.93 I'd like to round it to 20.90 How can I do that in Javascript ? Thanks! 回答1: Multiply the number by 10, round it and then divide the number by 10: var result = Math.round(20.93 * 10) / 10 回答2: I think this should work: number .toFixed(1); 回答3: var num= 20.93 num = Math.floor(num * 10) / 10; // 20.9 num = Math.ceil(num * 10) / 10; //21 回答4: I take it that you want the trailing zero. None of the answers give you that. It has to be a String to have the trailing

decimal custom format string with fixed precision and no period

给你一囗甜甜゛ 提交于 2019-12-11 12:51:52
问题 I've run into a situation which I think is beyond what you can do with custom format strings. But the code I've written is so gross I thought I would ask anyway. What I need is for a decimal to be displayed as either a 6 or 7 digit string, like so: number = 12345.67M (optional) tenthousands thousands hundreds tens ones tenths hundredths 1 2 3 4 5 6 7 Here's the code I've written to achieve this: public static string ConvertDecimalToString(decimal easting, int length) { var formatString = "{0

How to format a text input as a decimal in jquery

大憨熊 提交于 2019-12-11 11:23:00
问题 I have this jQuery code: $('#amount').change(function() { $('#amount').val( $('#amount').val().toFixed(2) ); }); // end .change() I need it to format the input in decmial format. Can it be done in jquery? Example: In a text box, if I enter 5 I want the value to be shown as 5.00 if I enter 5.1 I want the value to be shown as 5.10 if I enter 5.12 I want the value to be shown as 5.12 seems to be a simple need. I have to be missing something because I cannot imagine that javascript or jquery does

Division of a float/integer by a integer in jquery [duplicate]

早过忘川 提交于 2019-12-11 11:21:32
问题 This question already has answers here : Is floating point math broken? (31 answers) Closed 6 years ago . I am dividing a number( may be in amount format of XX.XX) by a integer. But when i do 6.6 / 6 or 3.3/3 , it gives 1.099999... , instead of 1.1. What i am doing is, I am dividing the amount equally among the number of people and if any remainder amount exists, I am adding that extra amount to one person among them. May be something of this kind, 104/6 will be divided as 17.85, 17.83, 17.83

Set number of decimal places to 0 if float is an integer (java)?

怎甘沉沦 提交于 2019-12-11 11:17:59
问题 I'm using a float to hold a score. The score can be an integer or decimal. By default, floats display as 0.0, 1.0, etc. If the number does not have a decimal, I need it to display as 0, 1, etc. If it does have a decimal, then I need to display the decimal. How might I do this? 回答1: String string; float n = 3.0f; if (n % 1 == 0) { string = String.valueOf((int) n); } else { string = String.valueOf(n); } System.out.println("Score: " + string); Warning: Untested code. ;) Ok, I've tested it and

Replacing all Hex numbers with Decimal representation

无人久伴 提交于 2019-12-11 10:54:38
问题 I have a string: string s ="My Favorite numbers are: 42, 0x42, 24 and 0x24" Is there a way using Regex to replace all hex numbers to their Decimal representation? For the example above, I would expect to get: "My Favorite numbers are: 42, 66, 24 and 36" . 回答1: Try this ( regular expression and conversion ): String source = "My Favorite numbers are: 42, 0x42, 24 and 0x24"; String result = Regex.Replace(source, @"0x(\d|[a-f]|[A-F])+", (MatchEvaluator) (match => Convert.ToInt32(match.Value, 16)

get sql server to warn about truncation / rounding

喜夏-厌秋 提交于 2019-12-11 10:46:53
问题 I am working on an ASP.NET C# accounting app and I am in the transition from using floats/doubles to using decimals to represent money. Now I want to control when and where rounding takes place. The way I understand it, SQL server rounds decimal data types differently than C# does. Is there some way to get SQL server (2008) to warn or fail if decimal values (or any kind of data) are getting truncated or rounded? Thanks, 回答1: Look at SET NUMERIC_ROUNDABORT and the table with SET ARITHABORT

What would be a good way of determining number of decimal places from a format string?

寵の児 提交于 2019-12-11 10:35:26
问题 I'd like a function that looks like this: int GetDecimalPlaces(string format, IFormatProvider formatProvider = null); The inputs would be exactly the same as those which can be legally passed to methods responsible for formatting numbers, e.g., double.ToString , decimal.ToString . The output would be an int indicating the lowest number of decimal places required by the format string. So here are a couple of example inputs/outputs I would expect (let's just say leaving formatProvider as null

C++ converting Variant Decimal to Double Value

試著忘記壹切 提交于 2019-12-11 09:58:52
问题 I have a _variant_t variable that contains a DECIMAL value from a database.. The problem is, I get an undefined value when I try to directly convert it to double type. For example, I have a decimal value = 1000.111 _varaint_t MyVar = getValueFromDatabase(); // MyVar is a decimal value = 1000.111 double MyDouble = (double) MyVar.dblVal; cout << MyDouble << endl; // Prints "4.941204871279e-318#DEN" What is the right way to convert Decimal to Double in C++? UPDATE: When I try this, _varaint_t

Why does my textarea (HTML) value show 12,000,000.11 but after parseFloat the value is only 12?

99封情书 提交于 2019-12-11 09:46:47
问题 I am attempting to develop a conversion website that takes a numeric value: 1,200.12 or 1.200,12 or 1200.12 or 1200,12 and have them all interpreted as 1200.12 by parseFloat. I would also like decimals to be able to be interpreted. 0.123 or 0,123 as 0.123 through a textarea and then parseFloat the number in order to perform calculations. These are the results I am getting: textarea input = 12,000.12 value after parseFloat = 12 Does parseFloat not recognize the formatting of the numbers? i get