double

Generating Random Doubles in Java [duplicate]

别等时光非礼了梦想. 提交于 2019-12-10 21:01:32
问题 This question already has answers here : Generate a random double in a range (5 answers) Closed 6 years ago . I'm a Java noob trying to generate a random double between -10 and 10 inclusive. I know with ints I would do the following: Random r = new Random(); int i = -10 + r.nextInt(21); However, with doubles, this doesn't work: Random r = new Random(); double i = -10 + r.nextDouble(21); Can someone please explain what to do in the case of doubles? 回答1: Try this: Random r = new Random();

TableView column data set to 2 decimal places

落爺英雄遲暮 提交于 2019-12-10 19:08:39
问题 I have 1 class file Nepokretnost.java in which constructor look like this: public Nepokretnost(String tipNepokretnosti, String zona, String pravo, double povrsina, int amortizacija, double osnovica, double kredit, double porez) { this.tipNepokretnosti = tipNepokretnosti; this.zona = zona; this.pravo = pravo; this.povrsina = povrsina; this.amortizacija = amortizacija; this.osnovica = osnovica; this.kredit = kredit; this.porez = porez; } Further, I have TableView with column for each of class

How to apply different formatting depending on whether number is positive or negative

心已入冬 提交于 2019-12-10 18:19:06
问题 I'm outputting a Double that can be either (+) or negative (-). If the number is a negative the symbol (-) is included automatically, is there a way to do this for positive numbers as well? The only (horrible) way I can do this is : If MyNumber <= 0 then string.Format("{0:0.00}", MyNumber) Else string.Format("+{0:0.00}", MyNumber) End If 回答1: You can use the section separator in your format: string.Format("{0:+0.00;-0.00}", num); The format before the semi-colon will be used for positive

Am I going crazy or is Math.Pow broken?

空扰寡人 提交于 2019-12-10 17:48:44
问题 I used the base converter from here and changed it to work with ulong values, but when converting large numbers, specifically numbers higher than 16677181699666568 it was returning incorrect values. I started looking into this and discovered that Math.Pow(3, 34) returns the value 16677181699666568, when actually 3^34 is 16677181699666569. This therefore throws a spanner in the works for me. I assume this is just an issue with double precision within the Pow method? Is my easiest fix just to

C++ - serialize double to binary file in little endian

流过昼夜 提交于 2019-12-10 17:46:39
问题 I'm trying to implement a function that writes double to binary file in little endian byte order. So far I have class BinaryWriter implementation: void BinaryWriter::open_file_stream( const String& path ) { // open output stream m_fstream.open( path.c_str(), std::ios_base::out | std::ios_base::binary); m_fstream.imbue(std::locale::classic()); } void BinaryWriter::write( int v ) { char data[4]; data[0] = static_cast<char>(v & 0xFF); data[1] = static_cast<char>((v >> 8) & 0xFF); data[2] =

Jquery: Prevent reloading page when pressing input type=submit

浪尽此生 提交于 2019-12-10 17:30:30
问题 I have the following code in a MVC 3 (Razor) project: <div class="user_info" id="id_user_info"> <script type="text/ecmascript" > $(function () { $('#buttonClear').live('submit', function (e) { return false; }); $('#buttonClear').bind('click', function () { $('username').value = ""; $('password').value = ""; }); </script> @using (Html.BeginForm("Login", "Account", FormMethod.Post, new { id = "loginForm" })) { <label for="username">Enter:</label> <input name="username" id="username" type="text"

Decimals not displaying when using Float or Double type

妖精的绣舞 提交于 2019-12-10 17:24:43
问题 My code is this: #include <iostream> int main() { using namespace std; const float dollar = 1.00; cout << "You have " << dollar*10.00 << " dollars." << endl; cin.get(); cin.get(); return 0; } I am a beginner to c++. I typed this code just playing around and assumed that the console would display "You have 10.00 dollars, but it actually displays that I have "10" and not "10.00" dollars. Could someone tell me why this is? 回答1: Since you are dealing with dollar amounts, you can set the following

Money Format Number VB.NET

依然范特西╮ 提交于 2019-12-10 17:24:10
问题 I'm trying to convert a mathematical result of money format example: Dim num1 As Integer = 2000 Dim num2 As Integer = 500 msgbox(cDbl(num1 + num2)) It only returns 2500, which I need to return my 2,500.00 if anyone has any idea how I would be very helpful thanks. 回答1: Your MsgBox shows you the value, but it hasn't formatted it, as you haven't asked it to. If you went a little further and formatted the result as a string , you'll get the format you desire, e.g: Dim num1 As Double = 2000 Dim

String formatting: negative/positive floating point numbers

徘徊边缘 提交于 2019-12-10 17:24:02
问题 How can I use String.Format in C# so doubles are displayed like this: example: ___-1.000 ____1.000 __100.123 -1000.321 _1000.214 etc... Where _ is space ( " " ); All I can do is String.Format("{0:F3}", -123.321); 回答1: You can use alignment: String.Format("{0,10:F3}", -123.321) where 10 is preffered length. See Composite Formatting. 回答2: Found a quick article, in short: String.Format("{0,10:0.0}", 123.4567); // " 123.5" String.Format("{0,-10:0.0}", 123.4567); // "123.5 " String.Format("{0,10:0

Floating point calculation gives different results with float than with double

独自空忆成欢 提交于 2019-12-10 17:03:19
问题 I have the following line of code. hero->onBeingHit(ENEMY_ATTACK_POINT * (1.0 - hero->getDefensePercent())); void onBeingHit(int decHP) method accepts integer number and updates health points. float getDefensePercent() method is a getter method returning the defense percent of a hero. ENEMY_ATTACK_POINT is a macro constant factor defined as #define ENEMY_ATTACK_POINT 20 . Let's say hero->getDefensePercent() returns 0.1 . So the calculation is 20 * (1.0 - 0.1) = 20 * (0.9) = 18 Whenever I