decimal-point

Printing to specific (runtime-determined) precision in Julia

≯℡__Kan透↙ 提交于 2020-01-03 19:18:42
问题 Given a value x and an integer n (assigned at runtime), I want to print x to exactly n digits after the decimal (after rounding if needed). print(round(x, n)) works fine for (x,n)=(3.141592, 3) but for (x,n)=(2.5,5) , it prints just 2.5 , not 2.50000 (5 digits after decimal point). If I knew n at runtime, say 5, I could do @printf("%.5f", x) But @printf being a macro needs n to be known at compile time. Is this possible using some show magic or something else? 回答1: Using the fresh new Format

Printing to specific (runtime-determined) precision in Julia

◇◆丶佛笑我妖孽 提交于 2020-01-03 19:18:26
问题 Given a value x and an integer n (assigned at runtime), I want to print x to exactly n digits after the decimal (after rounding if needed). print(round(x, n)) works fine for (x,n)=(3.141592, 3) but for (x,n)=(2.5,5) , it prints just 2.5 , not 2.50000 (5 digits after decimal point). If I knew n at runtime, say 5, I could do @printf("%.5f", x) But @printf being a macro needs n to be known at compile time. Is this possible using some show magic or something else? 回答1: Using the fresh new Format

String.Format - How can I format to x digits (regardless of decimal place)?

孤者浪人 提交于 2020-01-01 08:15:07
问题 I need to format a floating point number to x characters (6 in my case including the decimal point). My output also needs to include the sign of the number So given the inputs, here are the expected outputs 1.23456 => +1.2345 -12.34567 => -12.345 -0.123456 => -0.1234 1234.567 => +1234.5 Please assume there is always a decimal place before the last character. I.e. there will be no 12345.6 number input - the input will always be less than or equal to 9999.9 . I'm thinking this has to be done

String.Format - How can I format to x digits (regardless of decimal place)?

百般思念 提交于 2020-01-01 08:15:04
问题 I need to format a floating point number to x characters (6 in my case including the decimal point). My output also needs to include the sign of the number So given the inputs, here are the expected outputs 1.23456 => +1.2345 -12.34567 => -12.345 -0.123456 => -0.1234 1234.567 => +1234.5 Please assume there is always a decimal place before the last character. I.e. there will be no 12345.6 number input - the input will always be less than or equal to 9999.9 . I'm thinking this has to be done

Int to Decimal Conversion - Insert decimal point at specified location

不羁的心 提交于 2019-12-30 07:51:42
问题 I have the following int 7122960 I need to convert it to 71229.60 Any ideas on how to convert the int into a decimal and insert the decimal point in the correct location? 回答1: int i = 7122960; decimal d = (decimal)i / 100; 回答2: Simple math. double result = ((double)number) / 100.0; Although you may want to use decimal rather than double : decimal vs double! - Which one should I use and when? 回答3: Declare it as a decimal which uses the int variable and divide this by 100 int number = 700

How can I set the decimal separator to be a comma?

 ̄綄美尐妖づ 提交于 2019-12-28 02:07:09
问题 I would like to read and write pi as 3,141592 instead of 3.141592 , as using the comma is common in many European countries. How can I accomplish this with iostream s? In other words cout << 3.141592; should print 3,141592 to standard output. 回答1: You should use basic_ios::imbue to set the preferred locale. Take a look here: http://www.cplusplus.com/reference/ios/ios_base/imbue/ Locales allow you to use the preferred way by the user, so if a computer in Italy uses comma to separate decimal

How can I set the decimal separator to be a comma?

久未见 提交于 2019-12-28 02:07:01
问题 I would like to read and write pi as 3,141592 instead of 3.141592 , as using the comma is common in many European countries. How can I accomplish this with iostream s? In other words cout << 3.141592; should print 3,141592 to standard output. 回答1: You should use basic_ios::imbue to set the preferred locale. Take a look here: http://www.cplusplus.com/reference/ios/ios_base/imbue/ Locales allow you to use the preferred way by the user, so if a computer in Italy uses comma to separate decimal

In jQuery, what's the best way of formatting a number to 2 decimal places?

孤街浪徒 提交于 2019-12-27 13:39:34
问题 This is what I have right now: $("#number").val(parseFloat($("#number").val()).toFixed(2)); It looks messy to me. I don't think I'm chaining the functions correctly. Do I have to call it for each textbox, or can I create a separate function? 回答1: If you're doing this to several fields, or doing it quite often, then perhaps a plugin is the answer. Here's the beginnings of a jQuery plugin that formats the value of a field to two decimal places. It is triggered by the onchange event of the field

In jQuery, what's the best way of formatting a number to 2 decimal places?

淺唱寂寞╮ 提交于 2019-12-27 13:38:53
问题 This is what I have right now: $("#number").val(parseFloat($("#number").val()).toFixed(2)); It looks messy to me. I don't think I'm chaining the functions correctly. Do I have to call it for each textbox, or can I create a separate function? 回答1: If you're doing this to several fields, or doing it quite often, then perhaps a plugin is the answer. Here's the beginnings of a jQuery plugin that formats the value of a field to two decimal places. It is triggered by the onchange event of the field

How to prevent a decimal number being rounded to a whole number?

馋奶兔 提交于 2019-12-25 01:09:38
问题 I am using this code to get the numeric data from the file I am studying: [FileName,PathName]= uigetfile('*.txt*','Files to Study'); %fullfile puts pathname and filename into one file so it can be used to %import data from the chosen file file =fullfile(PathName,FileName); A = []; fid = fopen(file); tline = fgets(fid); while ischar(tline) parts = textscan(tline, '%d;'); if numel(parts{1}) > 0 A = [ A ; parts{:}' ]; end tline = fgets(fid); end fclose(fid); X = A(:,2) Y = A(:,7) It works fine,