Setting number of decimal points using double

Is it possible to set the number of decimal points
e.g.
double a = 2 * Math.random() - 1;
If I print this it would show something like 0.2342345434332.
What I want to do is show 0.234.
Is this possible?

here you go, you can run this small program to test the results for yourself...
import java.text.DecimalFormat;
public class floatTest{
     public static void main( String args[]){
DecimalFormat threeDig =  new DecimalFormat("0.000");
      double a = 2 * Math.random() - 1;
      //If I print this it would show something like 0.2342345434332.
      System.out.println("double a >" + a);
System.out.println("formatted>0.000 >" + threeDig.format(a)     );
     }//end of float
}//end of floatTEstok?

Similar Messages

  • Determine Number of Decimal Place using BigDecimal

    I was interested to have the following getNumberOfDecimalPlace function :
    System.out.println("0 = " + Utils.getNumberOfDecimalPlace(0)); // 0
    System.out.println("1.0 = " + Utils.getNumberOfDecimalPlace(1.0)); // 0
    System.out.println("1.01 = " + Utils.getNumberOfDecimalPlace(1.01)); // 2
    System.out.println("1.012 = " + Utils.getNumberOfDecimalPlace(1.012)); // 3
    System.out.println("0.01 = " + Utils.getNumberOfDecimalPlace(0.01)); // 2
    System.out.println("0.012 = " + Utils.getNumberOfDecimalPlace(0.012)); // 3
    I use the following code
        public static int getNumberOfDecimalPlace(double value) {
            final BigDecimal bigDecimal = new BigDecimal("" + value);
            final String s = bigDecimal.toPlainString();
            System.out.println(s);
            final int index = s.indexOf('.');
            if (index < 0) {
                return 0;
            return s.length() - 1 - index;
        }However, for case 0, 1.0, it doesn't work well. I expect, "0" as result. But they turned out to be "0.0" and "1.0". This will return "1" as result.
    0.0
    0 = 1
    1.0
    1.0 = 1
    1.01
    1.01 = 2
    1.012
    1.012 = 3
    0.01
    0.01 = 2
    0.012
    0.012 = 3
    Any solution?

    Please [don't cross-post!|http://stackoverflow.com/questions/2296110/determine-number-of-decimal-place-using-bigdecimal], it's considered rude. If you must do it, then at least link each post so that people can find out which answers you've already got in order to avoid duplicate work on our part.
    Please read [_How To Ask Questions The Smart Way_|http://www.catb.org/~esr/faqs/smart-questions.html].

  • Yielding the desired number of decimal places using the AVG function in t-sql

    Hello again.  Confused retired hobby coder having trouble setting up a scalar-valued function to return the desired number of decimal places from a AVG query.
    Whenever I run the following script I get the number of decimals I desire:
    Using the above I created a scalar-valued function as follows:
    Running this function as: SELECT [dbo].[TestHCIPartial] (1,3)  my return is -7.
    Can you help me with the function causing it to yield the answer to 6 decimal places?
    Thanks and regards, Minuend.

    You've not specified precision and scale in UDF return type. So you're leaving it to server to interpret it based on default settings
    If you want exactly up to four decimal places tweak udf as below
    ALTER FUNCTION..
    RETURNS decimal(20,4)
    AS
    Please Mark This As Answer if it solved your issue
    Please Vote This As Helpful if it helps to solve your issue
    Visakh
    My Wiki User Page
    My MSDN Page
    My Personal Blog
    My Facebook Page

  • How do I enter data in excel with a specified number of decimal points?

    I have several inputs that are being written into excel using the "Write to Spreadsheet file" vi. One of my inputs only writes to excel with 3 decimal points. I would like it to have four, how do I change this?

    Pop-up on the top input to the "Write to Spead...." labled "Format".
    Chnage it from the default of "%.3f" to "%.4f"
    Ben
    Ben Rayner
    Certified LabVIEW Developer
    www.DSAutomation.com
    Ben Rayner
    I am currently active on.. MainStream Preppers
    Rayner's Ridge is under construction

  • Finite Number of Data Points using DIO-32

    I have a system designed to scan a field of view and acquire data at each point in the field of view. The acquisition process does not begin until the system/clock is initiated. It is not continuously clocked and cannot be. I am trying to run this process at 25kHz using all four data ports of the DIO-32 board. At this rate, the VI seems to miss clock triggers and does not acquire the proper number of points. Currently, I can only run the process at 6kHz. I know the board can handle a faster data rate than this. Can someone please suggest a fix or provide an example VI that can do this. Will it help me to make my VI an executable using App builder (which I don't currently own)?
    Thanks,
    Bill

    Bill,
    Are you referring to the PCI-DIO-32HS? This board is in the 653x family of digital boards. It sounds like you are either trying to do pattern input with an external clock, or burst mode handshaking input with an external clock. You should check the 653x User Manual to determine which method is actually better for your application. You can view the certain conditions for which data will be latched in. There are also benchmarks on transfer rates in the appendix of the manual that you can view for comparison. Examples get installed with NI-DAQ and are available in the LabVIEW >> Daq >> Digital >> 653x.llb directory. There is an example for each of the types of operations I mentioned above. I recommend testing with a function generator first, so that you c
    an focus and isolate the issue with just the PCI-DIO-32HS board before moving on to your program and/or using your external devices with the board.
    Regards,
    Geneva L.
    Applications Engineer
    National Instruments
    http://www.ni.com/ask

  • Increase Number of Decimal Points on % summarized field in Cross Tab?

    Hi,
    I have a cross tab that I have added a "% of COUNT " summarized field to, but I want to show the percentage to 2 decimal places (DP) and make the rounding more accurate. Currently the rounding means the % values do not actually total to 100%.
    I assumed this would be as simple as changing the formatting of the field to increase the 'Decimals' property to 1.00 and the rounding to 0.01.
    However when I save the changes are not reflected in my cross tab? It still shows them with 0dp? e.g. 60%
    Is it possible to get % values in a cross tab showing to 2dp? e.g. 59.82%
    Thanks

    Please re-post if this is still an issue or purchase a case and have a dedicated support engineer work with your directly
    Formatting is for viewing only. Using formulae to manually round the values.

  • Round a double value to a specific number of decimal places?

    Hello,
    Is there standard java function which will round a double value to a specified number of decimal places? Something like:
    double d = 4.34523;
    d = round(d, 2);
    where d is finally assigned the value of 4.34?
    Thanks!
    -exits

    No, because doubles hold values in binary (as do all values in a computer, of course, but there's no additional stuff to indicate decimal values).
    If you want values with specific rounding rules in decimal, use java.math.BigDecimal.
    If you just want to format the number with a specified number of decimal digits, use java.text.DecimalNumber.

  • How to set the number of decimal places.

    Just wondering what and where the code would be to set the number of decimal places when calculating an answer.

    Depending on exactly what result you want you could alternatively format the result to a set number of decimal places.
    This is not so good for financial applications where they want the total of 10 transactions to match the total of each number displayed but can be kind of important for scientific ones.
    eg
    someNumber.ToString("N3"));
    Gives you three decimal places without changing the someNumber.
    If this turned out to be wpf then you have stringformat you can use on a binding.
    Hope that helps.
    Recent Technet articles:
    Property List Editing ;  
    Dynamic XAML

  • Conversion issues with decimal points from v5.1.5 to XI

    Several reports which were converted from v5.1.5 full client to XI Webi have numeric differences. When comparing v5.1.5  to XI, the sums on the columns don't exactly match. The cell format on these numbers is set to eight decimal points. When we converted, I noticed the cell format changed from number 0.00000000 in 5.1.5 to custom 0.00000000 in XI. I don't know if this is the reason the sums don't match or if it's something else.

    Hello Ellen,
    as you refer to a Webi problem I recommend to post this query to the [BusinessObjects Web Intelligence|SAP BusinessObjects Web Intelligence; forum.
    This forum is dedicated to topics related to the creation and design of Web Intelligence documents using the Web Intelligence Report Panels. This includes areas such as universe connectivity, prompts, charting, formatting, filters, and formulas.
    It is monitored by qualified technicians and you will get a faster response there.
    Also, all Webi queries remain in one place and thus can be easily searched in one place.
    Best regards,
    Falk

  • Currency Decimal points

    HI all,
    How to increase the decimal points of a particular currency. When I check in TCURX table maximum number of decimal points possible is 5. But I need to make decimal points as 7 for one currency. How can I do this?
    Regards
    Githen

    Hi
    This might be done through T.Code - OY04.
    Caution:- This configuration step has Cross- Client effects. So be careful.
    For interfaces, you can use function module BAPI_CURRENCY_CONV_TO_EXTERN_9.
    the below is information is from a thread..
    you can declare a variable with 7 decimals ..
    data : v_p type p decimals 7
    data : v_menge type p decimals 7
    Hope this helps
    Regards
    Shilpa

  • Automatic insertion of decimal point

    How do you make numbers automatically insert a decimal point? I want to input 12356 and I want it to appear in the cell as 123.56. In other words I want numbers to insert a decimal point in front of the last two digits I input. Having to input the decimal point myself really slows me down.
    to insert a decimal point

    Hi Jerry
    The fixed decimal point feature is available in Excel 2002 and is found under:
    Tools > options > edit tab
    about half way down click box next to fixed decimal point and you can also specify the number of decimal points.
    This fixed decimal point feature is then global for all sheets.
    In Excel 2007 click office button top left of screen
    in bottom right of drop down menu is a box named Excel options- click this
    then choose Advanced which is 5th option on LHS of menu and click second box in the menu next to Automatically insert a decimal point. You can again specify the number of points excel uses.
    I think this fixed decimal point feature must exist in Office 2004 as it appears in Excel 2002 and 2007.
    Hope this helps you. You don't miss it on a desktop keyboard where there is a numeric keypad but on a laptop it is a real pain to have to input the point on every entry.
    GG

  • GL - Increase decimal points in existing SOB

    Hi,
    We have a legacy system which interface to Oracle GL.
    In that system, the currency values are stored up to four decimal points (ex: 1009.7536).
    But in existing Oracle GL SOB is defined for two decimal points (ex: 1009.75)
    Can we change the number of decimal points interfaced or change in SOB.
    Or is there any way of capturing the remaining figures to Oracle GL.
    Appreciate your response.
    Madhura.

    Refer to note 143286.1 on Metalink

  • Change decimal point dynamically

    Is there a way to change dynamically the decimal point used in numeric control without restarting application?
    With decimal point localized, that's work only when restarting the application...
    Thanks

    Yes, you can by changing the Format string:
    Changing it from '%.;%f' will use a dot as decimal sign. If you change the format string into '%,;%f' will use comma.d
    You have to do this for every control you want to change, so creating some framework is usefull.
    Ton
    Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
    Nederlandse LabVIEW user groep www.lvug.nl
    My LabVIEW Ideas
    LabVIEW, programming like it should be!

  • How to set Decimal Point position from right to left Serial Read Evaluate Number within Range over RS-232

    I am new to Labview, I am communicating with a TQ8800 Torque meter via RS-232. The 16 digit data stream is as follows:
    D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
    Each digit indicate the following status :
    D0 End Word
    D1 & D8 Display reading, D1 = LSD, D8 = MSD
    For example :
    If the display reading
    is 1234, then D8 to D1 is :00001234
    D9 Decimal Point(DP), position from right to the left
    0 = No DP, 1= 1 DP, 2 = 2 DP, 3 = 3 DP
    D10 Polarity
    0 = Positive 1 = Negative
    D11 & D12 Annunciator for Display
    Kg cm = 81 LB inch = 82 N cm = 83
    D13 1
    D14 4
    D15 Start Word
    I am using a modified version of the basic_serial_write_read.vi. I am attempting to parse the 16 digit data stream extracting out the number and whether it is positive or negative. I am having trouble with the decimal point placement. Is there an example that could help me with this? After the number is parsed I am then comparing it to see if it is within +/- 9.2 N cm. If it is then the test passes. I am outputing the data to a file. I have included the vi. Also how can I check for different units of the annunciator. Any help would be appreciated.  Thank you.
    Attachments:
    basic_serial_read.vi ‏100 KB

    What is the definition of the End Word?  You will likely need to figure this out experimentally (the manual doesn't seem to define it).  Whatever it is, you should set that as the termination character and enable the termination character.  That will help you in keeping your messages in sync.  Just tell the VISA Reads to read more than a message should be, something like 30 should work nicely.
    The error you are seeing is a buffer overload.  This means that the instrument is sending data to you faster than you can process it.  So you need to figure out how to perform the read faster.  File IO is slow.  So you should put that into a seperate loop so that your loop does nothing but read the data from the port as quickly as the data comes in.  Use a queue to send the data from the reading loop to the logging loop.  This is known as a Producer/Consumer.
    And here is a cleaned up version of your code.  No need for your express VI or the formula node.  Some other general cleaning up also done to make it easier to read.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines
    Attachments:
    serial read.png ‏80 KB

  • How to use os x yosemite calculator - need decimal points

    I updated to OS X Yosemite version 10.10.2 for my Mac.  I cannot figure out how to use the calculator!  All I want is to add dollars and cents.  How do I get a decimal point?  I've searched the web and don't see this addressed.

    I double checked.  No decimal point anywhere!  I tried typing in a number with a period….nothing.
    Along the top row it has ASCII, Unicode, Show Binary buttons and 8, 10, 16.  I tried them all. 
    I have AND , OR, NOR, XOR, <<,>>, X<<Y, X>>Y, bite flip, word flip in the first two columns. Letters and numbers in the next three columns, and AC, C, RoL, RoR, 2's, and 1's buttons along with math operation symbols in the last 2 columns.
    I can't believe I can't just add up numbers with decimals when doing simple math!
    Any other ideas?
    Thanks.

Maybe you are looking for

  • How do I delete my icloud account when I have no internet service anymore

    I've just changed from an iphone 4s to a 5C. I want to sell my phone but my iphone 4 account has been disabled and it's irreversable. I need to delete my icloud account but as I can't go online, I can't turn off 'Find my Iphone.' Everything else has

  • Dead MS6330 after PCI IEEE1394 Controller install

    Hello, Yesterday I installed a PCI IEEE1394 Controller on a MS-6330 with 1300Mhz Athlon. After installing the necessary software and restarting my WinXP couldn't load. I tried removing and adding the newly installed card, the LAN and video cards. Now

  • Back-Up Software Question

    I need some help if anyone has an opinion. I just bought a LaCie external drive to back-up my documents, music, pictures, etc. It comes with back-up software, but I'm assuming there are better options out there. Does anyone have suggestions for good

  • Missing trading partner in FI document while disburse loan

    Dear All, We have the problem about trading partner in FI document. Our client wants the system to fill trading partner while post loan disbursement (FN5V). We try to maintain trading partner field in Control Tab in Business partner master data. But,

  • Function module for Indirect valuation

    Dear Experts, I need to calculate the 5 percent of 1st month basic salary in order for deduction. I am not able to get the basic pay details for indirect valuation wage types. Is there any function module to get it ? Please help Regards Bala