Search Results

Search found 1193 results on 48 pages for 'decimal'.

Page 3/48 | < Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • How do I convert decimal numbers to binary in Perl?

    - by David
    I am trying to make a program that converts decimal numbers or text into binary numbers in perl. The program asks for user input of a character or string , and then prints out the result to the console. How do I do this? My code I have been working on is below, but i cannot seem to fix it. print "Enter a number to convert: "; chomp($decimal = <STDIN>); print "\nConverting $number to binary...\n"; $remainder = $decimal%2; while($decimal > 0) { $decimal/2; print $remainder; }

    Read the article

  • C# float to decimal conversion

    - by Adrian4B
    Any smart way to convert a float like this: float f = 711989.98f; into a decimal (or double) without loosing precision? I've tried: decimal d = (decimal)f; decimal d1 = (decimal)(Math.Round(f,2)); decimal d2 = Convert.ToDecimal(f);

    Read the article

  • MS Access 2003 - Format a Number with Commas AND Auto-Decimal

    - by Emtucifor
    On a report I have a control which is bound to a column which can have up to 3 decimal places. I want the number to format with commas separating thousands and millions, but I also want the number of decimal places to be automatic, so that if there is no decimal portion then no decimal at all is shown. 1234.567 -> 1,234.567 1234.560 -> 1,234.56 1234.500 -> 1,234.5 1234.000 -> 1,234 General format will give me the auto decimal places but no commas. Standard format gives the comma but is fixed to 2 decimal places. Doing my own =Format(Number, "#,##0.#") leaves the decimal point in and doesn't align properly, with extra space on the right of the number. Do I have to write my own VB function to give the format I want? It seems silly that Access (apparently) can't do this out of the box. This also seems really horrible: =Replace(Replace(Replace(Replace(Replace( _ Format(Number, "#,##0.000") & "x", "0x", ""), "0x", ""), "0x, ""), ".x", ""), "x", "")

    Read the article

  • SQL SERVER – Excel Losing Decimal Values When Value Pasted from SSMS ResultSet

    - by pinaldave
    No! It is not a SQL Server Issue or SSMS issue. It is how things work. There is a simple trick to resolve this issue. It is very common when users are coping the resultset to Excel, the floating point or decimals are missed. The solution is very much simple and it requires a small adjustment in the Excel. By default Excel is very smart and when it detects the value which is getting pasted is numeric it changes the column format to accommodate that. Now as Zero which are training any digit after decimal points have no value, Excel automatically hides it. To prevent this to happen user has to convert columns to text format so it can preserve the formatting. Here is how you can do it. Select the corner between A and 1 and Right Click on it. It will select complete spreadsheet. If you want to change the format of any column you can select an individual column the same way. In the menu Click on Format Cells… It will bring up the following menu. Here by default the selected column will be General, change that to Text. It will change the format of all the cells to Text. Now once again paste the values from SSMS to the Excel. This time it will preserve the decimal values from SSMS. Solved! Any other trick you do you know to preserve the decimal values? Leave a comment please. Reference: Pinal Dave (http://blog.SQLAuthority.com) Filed under: PostADay, SQL, SQL Authority, SQL Query, SQL Server, SQL Tips and Tricks, SQL Utility, T SQL, Technology Tagged: Excel

    Read the article

  • Calculating pi using infinite series in C#

    - by Jonathan Chan
    Hi! I tried to write the following program in C# to calculate pi using infinite recursion, but I keep getting confused about integer/double/decimal division. I really have no clue why this isn't working, so pardon me for my lack of understanding of strongly typed stuff, as I'm still learning C#. Thanks in advance! using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { public static int Main(string[] args) { int numeratornext = 2; int denominatornext = 5; decimal findto = 100.0M; decimal pi = 0.0M; decimal halfpi = 1.0M; int seriesnum = 1; int seriesden = 3; for (int i = 0; i < findto; i++) { halfpi += Decimal.Divide((decimal)seriesnum, (decimal)seriesden); //System.Console.WriteLine(Decimal.Divide((decimal)seriesnum, (decimal)seriesden).ToString()); seriesnum *= numeratornext; seriesden *= denominatornext; numeratornext++; denominatornext += 2; } pi = halfpi * 2; System.Console.WriteLine(pi.ToString()); System.Console.ReadLine(); return 0; } } }

    Read the article

  • Is using decimal ranges in a switch impossible in C#?

    - by phobia
    I'm justing starting out learning C# and I've become stuck at something very basic. For my first "app" I thought I'd go for something simple, so I decided for a BMI calculator. The BMI is calculated into a decimal type which I'm now trying to use in a switch statement, but aparently decimal can't be used in a switch? What would be the C# solution for this: decimal bmi = calculate_bmi(h, w); switch (bmi) { case < 18.5: bmi_description = "underweight."; break; case > 25: bmi_description = "overweight"; case > 30: bmi_description = "very overweight"; case > 40: bmi_description = "extreme overweight"; break; }

    Read the article

  • Why does a C# System.Decimal remember trailing zeros?

    - by Rob Davey
    Is there a reason that a C# System.Decimal remembers the number of trailing zeros it was entered with? See the following example: public void DoSomething() { decimal dec1 = 0.5M; decimal dec2 = 0.50M; Console.WriteLine(dec1); //Output: 0.5 Console.WriteLine(dec2); //Output: 0.50 Console.WriteLine(dec1 == dec2); //Output: True } The decimals are classed as equal, yet dec2 remembers that it was entered with an additional zero. What is the reason/purpose for this?

    Read the article

  • Working with Decimal fields in SSIS

    - by CoffeeAddict
    I'm using SQL Server 2008 w/SP2. I've got an incoming decimal(9,2) field incoming through my OLE DB transformation to my recordset destination transformation. It's like it's reading it as something other than a decimal? I don't know..I'm not an SSIS guru. So continuing on...the problem I have starts here with me trying to stuff the value into a variable for this decimal field. In a foreach loop, I have a variable to represent this decimal field so I can work with it. The first problem that I believe is pretty well known is SSIS variables do not have a decimal type. And from my own testing and what I've read out there, people are using type object for the variable to make SSIS "happy" with decimal values? It makes mine happy. But, then in my foreach loop, I have a for loop. And inside that I'm using an E*xecute SQL Task transformation*. In it, I need to create a parameter mapping to my variable so I can work with that decimal field in my T-SQL call in here. So now I see a type decimal for the parameter and use it and set that to point to my variable. When I run SSIS and it hits my SQL call, I get this in my output window.: The type is not supported.DBTYPE_DECIMAL So I am hitting a wall here. All I wanna do is work with a decimal!!!

    Read the article

  • JavaScript: Rounding to two decimal places. Not less than two

    - by Abs
    Hello all, I have this line of code which rounds my numbers to 2 decimal places. But the thing is I get numbers like this. 10.8, 2.4 etc. These are not my idea of 2 decimal places so how I can improve this: Math.round(price*Math.pow(10,2))/Math.pow(10,2); I want numbers like 10.80, 2.40 etc. Use of JQuery is fine with me. Thanks for any help.

    Read the article

  • SQL SERVER – Puzzle – Challenge – Error While Converting Money to Decimal

    - by pinaldave
    Earlier I wrote SQL SERVER – Challenge – Puzzle – Usage of FAST Hint and I did receive some good comments. Here is another question to tease your mind. Run following script and you will see that it will thrown an error. DECLARE @mymoney MONEY; SET @mymoney = 12345.67; SELECT CAST(@mymoney AS DECIMAL(5,2)) MoneyInt; GO The datatype of money is also visually look similar to the decimal, why it would throw following error: Msg 8115, Level 16, State 8, Line 3 Arithmetic overflow error converting money to data type numeric. Please leave a comment with explanation and I will post a your answer on this blog with due credit. Reference: Pinal Dave (http://blog.sqlauthority.com) Filed under: Pinal Dave, SQL, SQL Authority, SQL Error Messages, SQL Puzzle, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQLServer, T SQL, Technology

    Read the article

  • asp.net compare validators to allow comma and dot (both!) as decimal separator

    - by DanC
    I am using a compare validator, which validates that the entered number is a valid double and also validates it against a given value (greater than zero). I am validating money amounts. Because of the location where the app is used, the locale sets the comma as the decimal separator. The problem is that when a user enters the value using the numeric keyboard, the number gets written with the dot as decimal separator, and is rejected by the validation. I'd like to have this validation done before triggering a postback (like a customvalidator would) and accepting both separators. Any ideas? Thanks

    Read the article

  • Javascript Regular expression to enforce 2 digits after decimal points

    - by Manas Saha
    I need to validate a text box where users are supposed to enter numeric values with 2 decimal places. I am validating the text in client side javascript. The validation will pass only if the number has precisely 2 decimal places, and there is at least 1 digit before the decimal point. (could be zero) the number before the decimal point can be 0, but can not be multiple zeroes. like 00 or 000 The number before the decimal point can not begin with more than 1 zero. Example of Passed validation: 0.01 0.12 111.23 1234.56 012345.67 123.00 0.00 Example of failed validation .12 1.1 0.0 00.00 1234. 1234.567 1234 00123.45 abcd.12 12a4.56 1234.5A I have tried with [0-9][.][0-9][0-9]$ But it is allowing alphabets before decimal point. like 12a4.56 Can anyone please fix the expression? thanks a lot in advance.

    Read the article

  • EF4 Code First Control Unicode and Decimal Precision, Scale with Attributes

    - by Dane Morgridge
    There are several attributes available when using code first with the Entity Framework 4 CTP5 Code First option.  When working with strings you can use [MaxLength(length)] to control the length and [Required] will work on all properties.  But there are a few things missing. By default all string will be created using unicode so you will get nvarchar instead of varchar.  You can change this using the fluent API or you can create an attribute to make the change.  If you have a lot of properties, the attribute will be much easier and require less code. You will need to add two classes to your project to create the attribute itself: 1: public class UnicodeAttribute : Attribute 2: { 3: bool _isUnicode; 4:  5: public UnicodeAttribute(bool isUnicode) 6: { 7: _isUnicode = isUnicode; 8: } 9:  10: public bool IsUnicode { get { return _isUnicode; } } 11: } 12:  13: public class UnicodeAttributeConvention : AttributeConfigurationConvention<PropertyInfo, StringPropertyConfiguration, UnicodeAttribute> 14: { 15: public override void Apply(PropertyInfo memberInfo, StringPropertyConfiguration configuration, UnicodeAttribute attribute) 16: { 17: configuration.IsUnicode = attribute.IsUnicode; 18: } 19: } The UnicodeAttribue class gives you a [Unicode] attribute that you can use on your properties and the UnicodeAttributeConvention will tell EF how to handle the attribute. You will need to add a line to the OnModelCreating method inside your context for EF to recognize the attribute: 1: protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) 2: { 3: modelBuilder.Conventions.Add(new UnicodeAttributeConvention()); 4: base.OnModelCreating(modelBuilder); 5: } Once you have this done, you can use the attribute in your classes to make sure that you get database types of varchar instead of nvarchar: 1: [Unicode(false)] 2: public string Name { get; set; }   Another option that is missing is the ability to set the precision and scale on a decimal.  By default decimals get created as (18,0).  If you need decimals to be something like (9,2) then you can once again use the fluent API or create a custom attribute.  As with the unicode attribute, you will need to add two classes to your project: 1: public class DecimalPrecisionAttribute : Attribute 2: { 3: int _precision; 4: private int _scale; 5:  6: public DecimalPrecisionAttribute(int precision, int scale) 7: { 8: _precision = precision; 9: _scale = scale; 10: } 11:  12: public int Precision { get { return _precision; } } 13: public int Scale { get { return _scale; } } 14: } 15:  16: public class DecimalPrecisionAttributeConvention : AttributeConfigurationConvention<PropertyInfo, DecimalPropertyConfiguration, DecimalPrecisionAttribute> 17: { 18: public override void Apply(PropertyInfo memberInfo, DecimalPropertyConfiguration configuration, DecimalPrecisionAttribute attribute) 19: { 20: configuration.Precision = Convert.ToByte(attribute.Precision); 21: configuration.Scale = Convert.ToByte(attribute.Scale); 22:  23: } 24: } Add your line to the OnModelCreating: 1: protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) 2: { 3: modelBuilder.Conventions.Add(new UnicodeAttributeConvention()); 4: modelBuilder.Conventions.Add(new DecimalPrecisionAttributeConvention()); 5: base.OnModelCreating(modelBuilder); 6: } Now you can use the following on your properties: 1: [DecimalPrecision(9,2)] 2: public decimal Cost { get; set; } Both these options use the same concepts so if there are other attributes that you want to use, you can create them quite simply.  The key to it all is the PropertyConfiguration classes.   If there is a class for the datatype, then you should be able to write an attribute to set almost everything you need.  You could also create a single attribute to encapsulate all of the possible string combinations instead of having multiple attributes on each property. All in all, I am loving code first and having attributes to control database generation instead of using the fluent API is huge and saves me a great deal of time.

    Read the article

  • Computer Arithmetic - Binary for Decimal Numbers

    - by MarkPearl
    This may be of use to someone else doing this course… The Problem In the section on Computer Arithmetic it gives an example of converting -7.6875 to IEEE floating point format. I understand all the steps except for the first one, where it does the following... 7.6875 (base 10) = 111.1011 (base 2) I don't understand the conversion - I realize that 111 (base 2) = 7 (base 10), but how does the .6875 part relate to the .1011? Or am I totally off track with this? The Solution The fractional part of the decimal to binary conversion is done as follows: 0.6875 x 2 = 1.375 = 0.375 + 1 (Keep the 1 separate) 0.375 x 2   = 0.75   = 0.75    + 0 0.75 x 2    = 1.5      = 0.5      + 1 0.5 x 2     = 1.0       = 0.0       + 1 The bit pattern of 0s and 1s on the right-hand side gives you the fractional part. So 0.6875 (base 10) = .1011 (Base 2) See also Stallings, chapter 19.

    Read the article

  • Specify XML schema data type of decimal or blank

    - by Jeremy Stein
    Is there a way in an XML schema to specify that an element may contain either an empty string or a decimal? If I specify the type as xs:decimal like this: <xs:element name="Sample" type="xs:decimal" /> then a blank value would not pass validation: <Sample/> (I realize that the best way to indicate no value would be to not include the element, but I was wondering if there was a way to allow blank or decimal.)

    Read the article

  • Is there a literal notation for decimal in IronPython?

    - by jeroenh
    suppose I have the following IronPython script: def Calculate(input): return input * 1.21 When called from C# with a decimal, this function returns a double: var python = Python.CreateRuntime(); dynamic engine = python.UseFile("mypythonscript.py") decimal input = 100M; // input is of type Decimal // next line throws RuntimeBinderException: // "cannot implicitly convert double to decimal" decimal result = engine.Calculate(input); I seem to have two options: First, I could cast at the C# side: seems like a no-go as I might loose precision. decimal result = (decimal)engine.Calculate(input); Second option is to use System.Decimal in the python script: works, but pollutes the script, which should be understandable for my users... import clr import System def CalculateVAT(amount): return amount * System.Decimal(1.21) Is there a way to tell the DLR that the number 1.21 should be interpreted as a Decimal, much like I would use the '1.21M' notation in C#?

    Read the article

  • Errors in Decimal Calcs within def clean method?

    - by allanhenderson
    I'm attempting a few simple calculations in a def clean method following validation (basically spitting out a euro conversion of retrieved uk product price on the fly). I keep getting a TypeError. Full error reads: Cannot convert {'product': , 'invoice': , 'order_discount': Decimal("0.00"), 'order_price': {...}, 'order_adjust': None, 'order_value': None, 'DELETE': False, 'id': 92, 'quantity': 8} to Decimal so I guess django is passing through the entire cleaned_data form to Decimal method. Not sure where I'm going wrong - the code I'm working with is: def clean_order_price(self): cleaned_data = self.cleaned_data data = self.data order_price = cleaned_data.get("order_price") if not order_price: try: existing_price = ProductCostPrice.objects.get(supplier=data['supplier'], product_id=cleaned_data['product'], is_latest=True) except ProductCostPrice.DoesNotExist: existing_price = None if not existing_price: raise forms.ValidationError('No match found, please enter new price') else: if data['invoice_type'] == 1: return existing_price.cost_price_gross elif data['invoice_type'] == 2: exchange = EuroExchangeRate.objects.latest('exchange_date') calc = exchange.exchange_rate * float(existing_price.cost_price_gross) calc = Decimal(str(calc)) return calc return cleaned_data If the invoice is of type 2 (a euro invoice) then the system should grab the latest exchange rate and apply that to the matching UK pound price pulled through to get euro result. Should performing a decimal conversion be a problem within def clean method? Thanks

    Read the article

  • Formula to format a cell to subtract one decimal place from another cell and have the calculated results displayed

    - by user242618
    I have a value in one cell that has four decimal places, in the cell below, I have the metric conversion formula. I want the cell below, with the metric conversion formula to display one less decimal place than the cell above. For example, if the English measurement is .#### (4 decimal places), I need the conversion cell to display .### (3 decimal places) and if the English measurement is .### (3 decimal places), I need the conversion cell to display .### ( decimal places), and so on. How can I do this?

    Read the article

< Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >